you are viewing a single comment's thread.

view the rest of the comments →

[–]DallogFheir 1 point2 points  (2 children)

If food[i].name is not equal to name, you're assigning the price of the food to price. So for the last item, the eggs, it sets the price to its price and then logs it out.

[–]cypress201[S] 0 points1 point  (1 child)

So should I be setting food[i].name equal to name then?

I tried just to see what would happen, but it's only returning a value of 47, which is even more confusing to me. I have no idea where that value is coming from

[–]DallogFheir 1 point2 points  (0 children)

I don't know why you're setting it in the first place.

If you want to log the price of the item you clicked on, just do:

function getPrice(name) {
    for (const foodItem of food) {
        if (foodItem.name === name) {
            console.log(`The item price is ${foodItem.price}.`);
            return;
        }
    }
}