Learn Basic OOP by Building a Shopping Cart - Step 24

Tell us what’s happening:

On step 23 we are creating
this.items.forEach((dessert) => {
totalCountPerProduct[dessert.id] = totalCountPerProduct[dessert.id] + 1

and on step 24 we are finding out about small bag and when trying to fix the bug one variable has change and seems that it’s not possible to pass through tests.
To fix this, you can use the || operator to set the value to 0 if it doesn’t exist. Wrap your right-hand totalCountPerProduct[data.id] in parentheses, and add || 0 to the end of the expression.

Before it was totalCountPerProduct[dessert.id] and in step 24 it is totalCountPerProduct[data.id].

Do i miss something?

Your code so far

const cartContainer = document.getElementById(“cart-container”);
const productsContainer = document.getElementById(“products-container”);
const dessertCards = document.getElementById(“dessert-card-container”);
const cartBtn = document.getElementById(“cart-btn”);
const clearCartBtn = document.getElementById(“clear-cart-btn”);
const totalNumberOfItems = document.getElementById(“total-items”);
const cartSubTotal = document.getElementById(“subtotal”);
const cartTaxes = document.getElementById(“taxes”);
const cartTotal = document.getElementById(“total”);
const showHideCartSpan = document.getElementById(“show-hide-cart”);
let isCartShowing = false;

const products = [
{
id: 1,
name: “Vanilla Cupcakes (6 Pack)”,
price: 12.99,
category: “Cupcake”,
},
{
id: 2,
name: “French Macaroon”,
price: 3.99,
category: “Macaroon”,
},
{
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 Macaroons (4 Pack)”,
price: 9.99,
category: “Macaroon”,
},
{
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 Macaroons (5 Pack)”,
price: 11.99,
category: “Macaroon”,
},
{
id: 12,
name: “Lemon Cupcakes (4 Pack)”,
price: 12.99,
category: “Cupcake”,
},
];

products.forEach(
({ name, id, price, category }) => {
dessertCards.innerHTML += <div class="dessert-card"> <h2>${name}</h2> <p class="dessert-price">$${price}</p> <p class="product-category">Category: ${category}</p> <button id="${id}" class="btn add-to-cart-btn">Add to cart </button> </div> ;
}
);

class ShoppingCart {
constructor() {
this.items = ;
this.total = 0;
this.taxRate = 8.25;
}

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

}
};

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

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

Challenge Information:

Learn Basic OOP by Building a Shopping Cart - Step 24

That quote is the key. Look how mutch of code You have wrapped in parentheses :wink:

Main issue is not parentheses, but what inside them and why it changed from dessert.id to data.id

I didn’t catched that, but it has been fixed alreday to dessert.id.
Besides that Your code above still have a bug. You have to return 0 if dessert doesn’t exist in object

1 Like

I just finished this challenge, you do not need to change dessert.id, it will pass.
The thing is, like Kabrax said, you have to return 0 if dessert doesn’t exist in object. Which part of code does that?
So what code needs to go into parentheses before you add +1 to it? :wink:
Think about the logic, your original output of adding one will not change if dessert exists right? And it will not add 1 to 0 since you tell it to return 0 before it can even add 1 if dessert does not exist.

1 Like

Thank you guys, now i got it!

1 Like