Learn Basic OOP by Building a Shopping Cart - Step 27

Tell us what’s happening:

I don’t understand why we are using the forEach() to track the count of each product ? because this logic will increment the count of all the products in the cart . what was the intention of the forEach loop ? to track the total number of items in the cart ?? because given element (X, Y, Z) if I add X, Y and Z then proceed to add two more Z the count for each subject would be increment as opposed to the specific element which would lead to undesired results

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;

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

    const currentProductCount = totalCountPerProduct[product.id];
    const currentProductCountSpan = document.getElementById(`product-count-for-id${id}`);


// 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/ Safari/537.36

Challenge Information:

Learn Basic OOP by Building a Shopping Cart - Step 27

  • yes, thats exactly whats happening here
  • all theese items would be in “items” and you are keeping track of by using this (“totalCountPerProduct[dessert.id]”)

does this help? happy coding :slight_smile: