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

const getActiveUsers = (DATA) => {
if (DATA == null || DATA.users == null) {
return null
}

let activeUsers = []

DATA.users.forEach((user) => {
if (user.accountActive === true) {
activeUsers.push(user)
}
})

return activeUsers
}

export default getActiveUsers

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

const getMostExpensiveProduct = (DATA) => {
if (DATA == null || DATA.products == null) {
return null
}
let mostExpProd // = undefined

for (let i = 0; i < DATA.products.length; i++) {
if (mostExpProd === undefined || DATA.products[i].price > mostExpProd.price) {
mostExpProd = DATA.products[i]
}
}

return mostExpProd
}

export default getMostExpensiveProduct

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

// passes
21 changes: 21 additions & 0 deletions data-and-functions-1/src/functions/getProductById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import DATA from '../DATA'

const getProductByID = (DATA, id) => {
if (DATA == null || DATA.products == null || id == null) {
return null
}
let productFound

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

if (currentProduct.id === id) {
productFound = currentProduct
}
}
return productFound
}

export default getProductByID

//passes
21 changes: 21 additions & 0 deletions data-and-functions-1/src/functions/getUserById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import DATA from '../DATA'

const getUserById = (DATA, id) => {
if (DATA == null || DATA.users == null || id == null) {
return null
}

let foundUser

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

if (currentUser.id === id) {
foundUser = currentUser
}
}
return foundUser
}
export default getUserById

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

const getProductsForOrder = (DATA, id) => {
if (DATA == null || DATA.products == null || id == null) {
throw new Error(('something is a little hairy here in getProductsForOrder...'))
}

let foundOrder

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

if (currentOrder.id === id) {
foundOrder = currentOrder
}
}
if (!foundOrder) {
return null
}

const currentProductId = foundOrder.id
const productsArray = []

const getOrderById = (DATA, id) => {
for (let j = 0; j < DATA.products.length; j++) {
const currentProduct = DATA.products[j]
if (currentProduct.id === currentProductId) {
}
return currentProduct
}
}

for (let i = 0; i < foundOrder.products.length; i++) {
let fetchedOrderId = getOrderById(DATA, id)
productsArray.push(fetchedOrderId)
}
return productsArray
}

export default getProductsForOrder

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

const getTotalPriceForOrder = (DATA, orderId) => {
if (DATA == null || DATA.products == null || DATA.id == null) {
throw new Error(('DATA, products, or id is null'))
}

for (let i = 0; i < DATA.orders.length; i++) {
const currentOrder = DATA.orders[i]
const totalPrice = getProductsForOrder(DATA, currentOrder.orderId).price

if (currentOrder.orderId === orderId) {
return totalPrice
}
if (!currentOrder.orderId) {
return null
}
}
}

export default getTotalPriceForOrder