Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions data-and-functions-1/src/functions/getActiveUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const getActiveUsers = (DATA, id) => {
if (DATA == null || DATA.users == null) {
return null
} else {
const activeUsers = []
DATA.users.forEach((user) => {
if (user.accountActive === true) {
activeUsers.push(user)
}
})
return activeUsers
}
}

export default getActiveUsers
24 changes: 24 additions & 0 deletions data-and-functions-1/src/functions/getMostExpensiveProduct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

const getMostExpensiveProduct = (DATA) => {
if (DATA == null || DATA.products == null) {
return null
} else {
let mostExpensive = DATA.products[0]
DATA.products.forEach((p) => {
if (mostExpensive === null || mostExpensive.price < p.price) {
mostExpensive = p
}
})
return mostExpensive
}
}

export default getMostExpensiveProduct

// return data.products.reduce((mostExpensive, p) => {
// if (p.price > mostExpensive.price) {
// return p
// } else {
// return mostExpensive
// }
// })
21 changes: 21 additions & 0 deletions data-and-functions-1/src/functions/getOrderInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import getUserById from './getUserById'
import getProductById from './getProductById'

const getOrderInfo = (DATA) => {
if (DATA == null || DATA.orders == null) {
return null
} else {
const orderInfoArr = []
DATA.orders.forEach((o) => {
const orderInfo = {
orderId: o.id,
userName: getUserById(DATA, o.userId).name,
price: getProductById(DATA, o.productId).price
}
orderInfoArr.push(orderInfo)
})
return orderInfoArr
}
}

export default getOrderInfo
14 changes: 14 additions & 0 deletions data-and-functions-1/src/functions/getProductById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const getProductById = (DATA, id) => {
if (DATA == null || DATA.users == null || id == null) {
return null
}
let product
DATA.products.forEach((p) => {
if (p.id === id) {
product = p
}
})
return product
}

export default getProductById
16 changes: 16 additions & 0 deletions data-and-functions-1/src/functions/getUserById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const getUserById = (DATA, id) => {
if (DATA == null || DATA.users == null || id == null) {
return null
}

let user

DATA.users.forEach((u) => {
if (u.id === id) {
user = u
}
})
return user
}

export default getUserById
15 changes: 15 additions & 0 deletions data-and-functions-3/src/functions/addOrder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

const addOrder = (DATA, order) => {
if (!DATA || !DATA.orders || !order || typeof order !== 'object' || order.id === true) {
throw new Error('Error')
}
const getNewId = DATA.orders.id.length + 1

const newOrder = { id: getNewId, userId: 1, products: 1 }

DATA.orders.push(newOrder)

return newOrder
}

export default addOrder
12 changes: 12 additions & 0 deletions data-and-functions-3/src/functions/addProduct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

const addProduct = (DATA, product) => {
if (!DATA || !DATA.products || !product || typeof product !== 'object' ) {
throw new Error('Error')
}

if (product.id === true) {
throw new Error('error')
}
}

export default addProduct