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 dump the else block if you want.

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.

You've already got a boolean. No need to create another one:

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 {
let mostExpensive = data.products[0]
data.products.forEach((p) => {
if (mostExpensive.price < p.price) {
mostExpensive = p
}
})
return mostExpensive
}
}

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 getProductById from './getProductById'
import getUserById from './getUserById'

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

Choose a reason for hiding this comment

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

Try writing this as a map since you've got the basics down.

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

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

Choose a reason for hiding this comment

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

Nice use of find. You can drop the block and use an implicit return if you want. You can also drop the parens around (p) since you only have one param in your lambda.

return p.id === id
})
}
}

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

export default getUserById
11 changes: 11 additions & 0 deletions data-and-functions-2/src/functions/getProductById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const getProductById = (data, id) => {
if (data == null || data.products == null || id == null) {
return null
} else {
return data.products.find((p) => {
return 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('Missing 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.

You can totally do the test the way you have it, but you could also just shorten it to:

if (!order) {

return null
}
const productArray = []
order.products.forEach((p) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use map here as well.

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.

Try importing getProductsForOrder instead and just use that.


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

export default getTotalPriceForOrder