Stuck on this JavaScript function trying to figure out the cartPrice and the priceLookup?
I'm new to the coding world and currently I'm stuck on trying to figure the question 1 & 3. Can someone help me in the right direction thanks.
let items = [
{
itemName: "Effective Programming Habits",
type: "book",
price: 13.99
},
{
itemName: "Creation 3005",
type: "computer",
price: 299.99
},
{
itemName: "Finding Your Center",
type: "book",
price: 15.00
}
]
// Prompt 1 : cartPrice
let sum = price
// Prompt 2 : mostExpensiveItemName
let maxItem = items.reduce((max, min) => max.price > min.price ? max : min);
console.log(maxItem.itemName)
console.log(maxItem)
// Prompt 3 : priceLookup
For priceLookup
:
function priceLookup(name) {
return items.find(item => item.itemName == name);
}
console.log(priceLookup("Creation 3005").price)
what are we trying to achieve?
@Summit I would like to get the functions to get;
1. total cartPrice
2.priceLookup
@techdog24 use a for loop ig
https://repl.it/@TheSummit3145/SuperiorPersonalOutcome#index.js
tell me if thats what u want
It worked but the it came back as cartPrice was not define so I modified it but still no luck. Thanks! my mod is below.
function cartPrice(price){
let price= 0;
for(let i = 0; i<items.length; i++)
{
price += items[i].price;
}
console.log(price);
}@Summit
@techdog24 in your modified function, the paramter in the funciton has to be the array name (in your case it is item), because we are passing in the cart/array data
it should be
function cartPrice(items){
let price= 0;
for(let i = 0; i<items.length; i++)
{
price += items[i].price;
}
console.log(price);
}
ok I'll adjust it thanks. @Summit
@techdog24 if it works can you mark my post as the solution?
@techdog24 YAY GOOD JOB!
check out the repl https://repl.it/@techdog24/Take-Home-School-Shopping-7 @Summit
What's the problem?
@Coder100 I would like to get the functions to get;
1. total cartPrice
2.priceLookup
maxtitem was able to give me the most expensive item in the cart. @Coder100
https://repl.it/@techdog24/Take-Home-School-Shopping-4