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
13 changes: 13 additions & 0 deletions data-and-functions-1/src/functions/getActiveUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import DATA from '../DATA'
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to pass in the data. The first parameter of all of your functions should be the data.


const getActiveUsers = (id) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a data parameter as the first parameter.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sanity check the parameters according to the getActiveUsers.test.js file.

let activeUserArray = []
DATA.users.forEach((u) => {
if (u.accountActive === true) {
activeUserArray.push(u)
}
})
return activeUserArray
}

export default getActiveUsers
17 changes: 17 additions & 0 deletions data-and-functions-1/src/functions/getMostExpensiveProduct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import DATA from '../DATA'
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the import.


const getMostExpensiveProduct = (data) => {
if (data == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This check is correct, but you need one more I think.

return null
}
let mostExpensiveProduct = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

There are many ways to solve this one. You need to declare a variable like you did, but let's not initialize it: let mostExpensiveProduct

DATA.products.forEach((m) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The forEach loop passes in the actual object as the first param to your function. Instead of calling it m, call it currentProduct

const currentProduct = data.products[m]
Copy link
Contributor

Choose a reason for hiding this comment

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

Delete this line.

if (currentProduct.price > mostExpensiveProduct) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Two things need to change here:

  1. You should test to see if mostExpensiveProduct == null. OR:
  2. Compare the prices of the currentProduct and the mostExpensiveProduct

If either of those are true, enter your if

mostExpensiveProduct = currentProduct
}
})
return mostExpensiveProduct
}

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

const getOrderInfo = (DATA) => {
if (DATA == null || DATA.orders == null) {
return null
}
const orderArray = []
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.

Replace the o parameter with currentOrder

const currentOrder = DATA.orders[o]
Copy link
Contributor

Choose a reason for hiding this comment

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

Delete this

const userName = getUserById(DATA, currentOrder.userId).name
const orderId = currentOrder.id
const price = getProductById(DATA, currentOrder.productId).price
const orderInfo = {
orderId: orderId,
userName: userName,
price: price
}
orderArray.push(orderInfo)
})
return orderArray
}

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

Choose a reason for hiding this comment

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

You don't need to import the data, since the test will pass it in as the first parameter.


const getProductById = (id) => {
Copy link
Contributor

@jcheroske jcheroske Aug 23, 2017

Choose a reason for hiding this comment

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

The expected function signature is:

const getProductById = (data, productId) => {

Copy link
Contributor

Choose a reason for hiding this comment

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

You also need to test to make sure data, data.products, and productId are not null.

let product
for (let n = 0; n < DATA.products.length; n += 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's standard practice to use the variable name i in a for loop.

if (DATA.products[n].id === id) {
product = DATA.products[n]
}
}
return product
}

export default getProductById
15 changes: 15 additions & 0 deletions data-and-functions-1/src/functions/getUserById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const getUserById = (DATA, userId) => {
let user
if (userId == null || DATA == null || DATA.users == null) {
return null
} else {
DATA.users.forEach((u) => {
if (u.userId === DATA.id) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't want to test against DATA.id. What is the correct comparison?

if (u.userId === ???) { // <-- What do we really want to compare `u.userId` against?

user = u
}
})
}
return user
}

export default getUserById