-
Notifications
You must be signed in to change notification settings - Fork 11
Data and functions 2 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
| const activeUsers = [] | ||
| data.users.forEach((u) => { | ||
| if (u.accountActive === true) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
| activeUsers.push(u) | ||
| } | ||
| }) | ||
| return activeUsers | ||
| } | ||
| } | ||
| export default getActiveUsers | ||
| 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| let mostExpensiveProduct = data.products[0] | ||
| data.products.forEach((p) => { | ||
| if (p.price > mostExpensiveProduct.price) { | ||
| mostExpensiveProduct = p | ||
| } | ||
| }) | ||
| return mostExpensiveProduct | ||
| } | ||
| } | ||
|
|
||
| export default getMostExpensiveProduct | ||
| 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 = [] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we've already got |
||
| 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 | ||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: Your call on that one though. |
||
| } | ||
| } | ||
| export default getProductById | ||
| 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 |
| 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 |
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing for or |
||
| return null | ||
| } | ||
| const productArray = [] | ||
| order.products.forEach((p) => { | ||
| productArray.push(getProductById(data, p)) | ||
| }) | ||
| return productArray | ||
| } | ||
|
|
||
| export default getProductsForOrder | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import getProductById from './getProductById' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Notice how similar this function ( |
||
|
|
||
| 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 | ||
There was a problem hiding this comment.
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
elseblock and it will make your code easier to read.