Learn Basic OOP by Building a Shopping Cart - Step 24

Tell us what’s happening:

Back again with another syntax issue.

I am wrapping this in parenthesis. I have also tried adding the end paren after the ||0, but still nothing is working.

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Learn Basic OOP by Building a Shopping Cart - Step 24

you should have the fallback inside these round parenthesis, so that if it is null or undefined then it is replaced with a 0

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

Thank you for the suggestion. This fails as well. the hint keeps telling me to wrap totalCountPerProduct[dessert.id] in parenthesis.

only the fallback, not the sum.
you add only the fallback into the parenthesis, and then add +1 outside when the value is determined


not required for the step, but important info:

if you have this, and the value is undefined you have undefined +1 || 0 which collapses to 0, when you want instead to have 1 as first value when counting a product

const totalCountPerProduct = {};
this.items.forEach((dessert) => {
totalCountPerProduct[dessert.id] = (totalCountPerProduct[dessert.id]) + 1 || 0;
Is this the syntax you mean?

you still need the fallback, and the fallback only not the sum, inside the parenthesis

I finally got it. For those of you wondering. Adding || 0 at the end is not necessarily the end you see in the examples.