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
16 changes: 16 additions & 0 deletions data-and-functions-1/src/functions/getActiveUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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.

This else can go too.

const activeUsers = []
for (let i = 0; i < data.users.length; i++) {
const currentUser = data.users[i]
if (currentUser.accountActive === true) {
activeUsers.push(currentUser)
}
}
return activeUsers
}
}

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

let mostExpensive = data.products[0]
for (let i = 1; i < data.products.length; i++) {
const currentProduct = data.products[i]
if (mostExpensive.price < currentProduct.price) {
mostExpensive = currentProduct
}
}
return mostExpensive
}

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

const getOrderInfo = (DATA) => {
if (DATA == null || DATA.orders == null) {
return null
}
const orderInfoArr = []

for (let i = 0; i < DATA.orders.length; i++) {
const currentOrder = DATA.orders[i]

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

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{
Copy link
Contributor

Choose a reason for hiding this comment

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

drop this else

return data.products.find((p) => {
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{
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 drop this else too.

return data.users.find((u) => u.id === id)
}
}

export default getUserById
Empty file.