Learn Basic OOP by Building a Shopping Cart - Step 24

Tell us what’s happening:

Unable to pass as I am getting error

You should wrap your right-hand totalCountPerProduct([dessert.id]) in parans, I tried totalCountPerProduct([dessert.id] + 1) || 0andtotalCountPerProduct([dessert.id]) || 0Not sure why is it not working when the error clearly saystotalCountPerProduct([dessert.id])` which I did but nothing.

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

  addItem(id, products) {
    const product = products.find((item) => item.id === id);
    const { name, price } = product;
    this.items.push(product);

    const totalCountPerProduct = {};
    this.items.forEach((dessert) => {
      totalCountPerProduct[dessert.id] = (totalCountPerProduct[dessert.id] + 1) || 0;
    })
  }

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36

Challenge Information:

Learn Basic OOP by Building a Shopping Cart - Step 24

Hi there. The hint says , wrap the above part right side in parentheses and add || and 0 in it.
Edit: It’s a boolean check if totalCountPerProduct` has an item (dessert.id) in it or (||) nothing (0) then the operation (+ 1) insure, that it have one.

that did not help as well

    this.items.forEach((dessert) => {
      totalCountPerProduct[dessert.id] = (totalCountPerProduct[dessert.id] + 1) || 0;
    })

You have add the closing paran after + 1 and also the OR operator and value 0.
Edit: instructions asking: Wrap your right-hand totalCountPerProduct[dessert.id] in parentheses,

I don’t understand, now I have code
totalCountPerProduct[dessert.id] = (totalCountPerProduct[dessert.id] )+ 1 || 0; but that doesn’t make sense because if (totalCountPerProduct[dessert.id] ) is undefined we cannot add +1 to it

Hey,

Yes exactly, if it’s undefined then you can’t add 1 to it. That’s why you should add your || 0 inside the parenthesis as well. Then, if totalCountPerProduct[dessert.id] is undefined then 0 will be added.
Hope this helps!

Like this?

 this.items.forEach((dessert) => {
      totalCountPerProduct[dessert.id] = (totalCountPerProduct[dessert.id] + 1 || 0);
    })

because that didn’t work,

You’re getting there. You shouldn’t add + 1 inside the parenthesis. The instructions also make it clear.
Good luck!

1 Like

thanks, it solved. One think I have huge issue is logic. I literally follow how question is written and I miss huge chunck of it can be other way around.

1 Like