Learn Basic OOP by Building a Shopping Cart - Step 55

Tell us what’s happening: I don’t see where is the error, I make a if statement but there is always an error

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 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",
 },
];

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] || 0) + 1;
   })

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

   currentProductCount > 1 
     ? currentProductCountSpan.textContent = `${currentProductCount}x`
     : productsContainer.innerHTML += `
     <div id=dessert${id} class="product">
       <p>
         <span class="product-count" id=product-count-for-id${id}></span>${name}
       </p>
       <p>${price}</p>
     </div>
     `;
 }

 getCounts() {
   return this.items.length;
 }

 clearCart() {
     if(!(this.items.length!==0)){
          alert("Your shopping cart is already empty");
          return ;
     }
 }

 calculateTaxes(amount) {
   return parseFloat(((this.taxRate / 100) * amount).toFixed(2));
 }

 calculateTotal() {
   const subTotal = this.items.reduce((total, item) => total + item.price, 0);
   const tax = this.calculateTaxes(subTotal);
   this.total = subTotal + tax;
   cartSubTotal.textContent = `$${subTotal.toFixed(2)}`;
   cartTaxes.textContent = `$${tax.toFixed(2)}`;
   cartTotal.textContent = `$${this.total.toFixed(2)}`;
   return this.total;
 }
};

const cart = new ShoppingCart();
const addToCartBtns = document.getElementsByClassName("add-to-cart-btn");

[...addToCartBtns].forEach(
 (btn) => {
   btn.addEventListener("click", (event) => {
     cart.addItem(Number(event.target.id), products);
     totalNumberOfItems.textContent = cart.getCounts();
     cart.calculateTotal();
   })
 }
);

cartBtn.addEventListener("click", () => {
 isCartShowing = !isCartShowing;
 showHideCartSpan.textContent = isCartShowing ? "Hide" : "Show";
 cartContainer.style.display = isCartShowing ? "block" : "none";
});```



> 

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.

```text
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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Learn Basic OOP by Building a Shopping Cart - Step 55

Hey, you have several errors here for one there are missing quotation marks on one of you attr. Also it looks like you wrote a function with out the ES6 unless this is a method and there no curly braces. Copy your code and run it in an IDE that will analyze and or debug the code, in this case I used CodePen. Good luck.

<p>
         <span class="product-count" id=product-count-for-id${id}></span>${name}
       </p>
       <p>${price}</p>
const cart = new ShoppingCart();
const addToCartBtns = document.getElementsByClassName("add-to-cart-btn");

why I should put again the quotation marks on these variables(price and id) because they are already in the syntax litteral? and cart is a instantiation of ShoppingCart

can someone help me ?

when you write html you write the attribute value in quotes, even if you are writing this in a template literal, that doesn’t exent from using correct html sintax

items is a property of Shopping class I don’t understand your referencely with html

Looking at the project, that looks like an issue in the project itself, not something for this step.

So let’s look at the code you added for this step:

This line:

Remember that 0 is a falsy value, so you can use the ! operator to check if the array is empty.

and the hint

You should create an if statement that checks if the items array is empty, using the negation operator.

The length of the array, this.items.length will be or 0 or a positive number, and they hint at using this without doing a comparison, but only that 0 is falsy and other numbers are truthy

1 Like

I solved this issue thank you for your help

First of all the line came back with errors, and secondly I`m not familiar with what you mean by is aready in a syntax literal. Also I stand corrected on the shopping cart call or instantiation, but it had errors as well.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.