Learn Basic OOP by Building a Shopping Cart - Step 23

I have already completed this step, but I need to understand the logic behind it. I don’t get how when creating a new property for the object totalCountPerProduct and updating the value, how do you add the current value of that property plus one when the current value does not have anything at first. Is the value undefined, so its undefined + 1, or is it automatically a value of zero, 0 + 1. Please explain.

Step 23 - In your forEach callback, you need to update the totalCountPerProduct object. Using the id of the current dessert as your property, update the value of the property to be the current value plus one. Do not use the addition assignment operator for this.

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) => {
      //solution is here
    })
  }

Hi @jobanibusiness

Each id key of the products object has a property number.

const products = [
  {
    id: 1,
    name: "Vanilla Cupcakes (6 Pack)",
    price: 12.99,
    category: "Cupcake",
  },
  {
    id: 2,
    name: "French Macaron",
    price: 3.99,
    category: "Macaron",
  },
  {
    id: 3,
    name: "Pumpkin Cupcake",
    price: 3.99,
    category: "Cupcake",
  },
  {
    id: 4,
    name: "Chocolate Cupcake",
    price: 5.99,
    category: "Cupcake",
  },
  {
    id: 5,
    name: "Chocolate Pretzels (4 Pack)",
    price: 10.99,
    category: "Pretzel",
  },
  {
    id: 6,
    name: "Strawberry Ice Cream",
    price: 2.99,
    category: "Ice Cream",
  },
  {
    id: 7,
    name: "Chocolate Macarons (4 Pack)",
    price: 9.99,
    category: "Macaron",
  },
  {
    id: 8,
    name: "Strawberry Pretzel",
    price: 4.99,
    category: "Pretzel",
  },
  {
    id: 9,
    name: "Butter Pecan Ice Cream",
    price: 2.99,
    category: "Ice Cream",
  },
  {
    id: 10,
    name: "Rocky Road Ice Cream",
    price: 2.99,
    category: "Ice Cream",
  },
  {
    id: 11,
    name: "Vanilla Macarons (5 Pack)",
    price: 11.99,
    category: "Macaron",
  },
  {
    id: 12,
    name: "Lemon Cupcakes (4 Pack)",
    price: 12.99,
    category: "Cupcake",
  },
];

Happy coding

Can you address some of this terminology totalCountPerProduct is a variable and its assigned an object. so it cant be undefined here. in other words is this an empty object. Was this what you were trying to solve?

Yes, I know, but my question is what the “current value” is when the questions asks to “update the value of the property to be the current value plus one.” Is it 0?

To not give away the solution, here is the pseudo code.

const totalCountPerProduct = {};
this.items.forEach((dessert) => {
      totalCountPerProduct[property] = totalCountPerProduct[property] + 1;
                                               //here ^
    })

I am trying to understand what the “current value” is as its value in the question “update the value of the property to be the current value plus one.” I am not asking for the solution, I’m asking what that current value is when you are just adding exactly one item to the cart.

pseudo code, I am trying my best to ask the question without giving the solution.
`
property: property + 1;

     //this^

`

The current value of the first object in the products array is 1.

key is id
property is 1

So are you saying that objects dont follow the 0 rule in this instance?

Step 24
You now have a small bug. When you try to access a property of an object and the property doesn’t exist, you get undefined. This means if the dessert isn’t already present in the totalCountPerProduct object, you end up trying to add 1 to undefined, which results in NaN.
To fix this, you can use the || operator to set the value to 0 if it doesn’t exist. Wrap your right-hand totalCountPerProduct[dessert.id] in parentheses, and add || 0 to the end of the expression.

1 Like

It makes sense now, thank you.

1 Like