Skip to content
Sign upLog in
← Back to Community
Stuck on this JavaScript function trying to figure out the cartPrice and the priceLookup?
Profile icon
techdog24

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

Voters
Profile icon
ChristianSaravi
Profile icon
JaninaVillarroe
Profile icon
techdog24
Comments
hotnewtop
Profile icon
techdog24
Profile icon
Coder100

For priceLookup:

function priceLookup(name) { return items.find(item => item.itemName == name); } console.log(priceLookup("Creation 3005").price)
Profile icon
Summit

what are we trying to achieve?

Profile icon
techdog24

@Summit
I would like to get the functions to get;

  1. total cartPrice
    2.priceLookup
Profile icon
Summit
Profile icon
techdog24

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

Profile icon
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);
}

Profile icon
techdog24

ok I'll adjust it thanks.

@Summit

Profile icon
Summit

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

Profile icon
Summit

@techdog24
YAY GOOD JOB!

Profile icon
techdog24
Profile icon
ChristianSaravi

@Summit
it wont work for me :(

Profile icon
Coder100

What's the problem?

Profile icon
techdog24

@Coder100
I would like to get the functions to get;

  1. total cartPrice
    2.priceLookup
Profile icon
techdog24

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

@Coder100

Profile icon
Coder100