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) => {
if (data == null || data.users == null) {
return null
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get rid of the else block and it will make your code easier to read.

const activeUsers = []
data.users.forEach((u) => {
if (u.accountActive === true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since u.accountActive is a boolean, you can shorten your test to:

if (u.accountActive)

activeUsers.push(u)
}
})
return activeUsers
}
}
export default getActiveUsers
15 changes: 15 additions & 0 deletions data-and-functions-1/src/functions/getMostExpensiveProduct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const getMostExpensiveProduct = (data) => {
if (data == null || data.products == null) {
return null
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This else block can be eliminated as well.

let mostExpensiveProduct = data.products[0]
data.products.forEach((p) => {
if (p.price > mostExpensiveProduct.price) {
mostExpensiveProduct = p
}
})
return mostExpensiveProduct
}
}

export default getMostExpensiveProduct
20 changes: 20 additions & 0 deletions data-and-functions-1/src/functions/getOrderInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import getUserById from './getUserById'
import getProductById from './getProductById'

const getOrderInfo = (data) => {
if (data == null || data.orders == null) {
return null
} else {
const orders = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we've already got data.orders, and since the function is called getOrderInto, lets call this array orderInfos instead.

data.orders.forEach((o) => {
orders.push({
orderId: o.id,
price: getProductById(data, o.productId).price,
userName: getUserById(data, o.userId).name
})
})
return orders
}
}

export default getOrderInfo
8 changes: 8 additions & 0 deletions data-and-functions-1/src/functions/getProductById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const getProductById = (data, id) => {
if (data == null || data.products == null || id == null) {
return null
} else {
return data.products.find((p) => p.id === id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When your lambda function only takes one parameter, you can eliminate the parens if you want. I find it makes the code easier to read:

p => p.id === id

Your call on that one though.

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

export default getProductById
18 changes: 18 additions & 0 deletions data-and-functions-2/src/functions/getProductsForOrder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import getProductById from './getProductById'

const getProductsForOrder = (data, id) => {
if (data == null || id == null || data.orders == null) {
throw new Error('Do not have data or ID')
}
const order = data.orders.find((o) => o.id === id)
if (order === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing for undefined is usually not recomended. It's more of a style thing. I would do one of these tests instead:

if (!order) {

or

if (order == null) {

return null
}
const productArray = []
order.products.forEach((p) => {
productArray.push(getProductById(data, p))
})
return productArray
}

export default getProductsForOrder
18 changes: 18 additions & 0 deletions data-and-functions-2/src/functions/getTotalPriceForOrder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import getProductById from './getProductById'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice how similar this function (getTotalPriceForOrder) is to getProductsForOrder. Can you import that function and use it to get the total price?


const getTotalPriceForOrder = (data, id) => {
if (data == null || id == null || data.orders == null) {
throw new Error('There are no orders')
}
const order = data.orders.find((o) => o.id === id)
if (order === undefined) {
return null
}
let totalOrderPrice = 0
order.products.forEach((p) => {
totalOrderPrice += getProductById(data, p).price
})
return totalOrderPrice
}

export default getTotalPriceForOrder