Ask coding questions

← Back to all posts
Stuck on this JavaScript function trying to figure out the cartPrice and the priceLookup?
techdog24 (1)

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

Comments
hotnewtop
Coder100 (17090)

For priceLookup:

function priceLookup(name) {
  return items.find(item => item.itemName == name);
}

console.log(priceLookup("Creation 3005").price)
Summit (27)

what are we trying to achieve?

techdog24 (1)

@Summit I would like to get the functions to get;
1. total cartPrice
2.priceLookup

techdog24 (1)

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

Summit (27)

@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);
}

techdog24 (1)

ok I'll adjust it thanks. @Summit

Summit (27)

@techdog24 if it works can you mark my post as the solution?

Coder100 (17090)

What's the problem?

techdog24 (1)

@Coder100 I would like to get the functions to get;
1. total cartPrice
2.priceLookup

techdog24 (1)

maxtitem was able to give me the most expensive item in the cart. @Coder100