Learn Basic OOP by Building a Shopping Cart - Step 24

Tell us what’s happening:

Hello Everyone,
can some please point out what I am missing in this execution? The instruction says: " Wrap your right-hand totalCountPerProduct[dessert.id] in parentheses, and add || 0 to the end of the expression. "

As shown below, I have tried more than three methods to set this expression, but none of them are working?

Your code so far

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

/* 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;
    })
  }


//method two:

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

//method three:

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 ) || (totalCountPerProduct[dessert.id]+ 0 );
    })
  }
// User Editable Region
/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36

Challenge Information:

Learn Basic OOP by Building a Shopping Cart - Step 24

The instructions appear a little ambiguous but you should be adding the || 0 inside the parentheses (i.e. before +1). The point of this is that if totalCountPerProduct[dessert.id] doesn’t have a value, it will instead default to 0.

2 Likes

Thanks. I tried this method earlier on, but I removed the + 1 from the code block. With your hint, I factor out the +1 and it work out. Much appreciated

1 Like