Learn Basic OOP by Building a Shopping Cart - Step 25

Tell us what’s happening:

I am not sure why this step is not working please can someone help

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);
    
    if (product) {
        // Add product to the cart
        this.items.push(product);

        // Increment the count of this product in the cart
        this.totalCountPerProduct[product.id] = (this.totalCountPerProduct[product.id] || 0) + 1;
        let currentProductCount = this.totalCountPerProduct[product.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/129.0.0.0 Safari/537.36

Challenge Information:

Learn Basic OOP by Building a Shopping Cart - Step 25

You have two errors here.

  1. Use const to declare your variable.
  2. You are getting the value of totalCountPerProduct[product.id] and therefore, in this case, you do not need to use this. Use .value to get the value.
    const product = products.find((item) => item.id === id);
    
    if (product) {
        this.items.push(product);
        this.totalCountPerProduct[product.id] = (this.totalCountPerProduct[product.id] || 0) + 1;
        const currentProductCount = this.totalCountPerProduct[product.id]
    }
}

You corrected the first error, but you’re yet to correct the second one. You need to get the value.

addItem(id, products) {
    const product = products.find((item) => item.id === id);
    
    if (product) {
        this.items.push(product);
       this.totalCountPerProduct[product.id] = (this.totalCountPerProduct[product.id] || 0) + 1;
        const currentProductCount = this.totalCountPerProduct[product.id]
    }
}

Please don’t forget to talk to the humans trying to help instead of just posting code!

addItem(id, products) {
    const product = products.find((item) => item.id === id);
    
    if (product) {
        this.items.push(product);
       this.totalCountPerProduct[product.id] = (this.totalCountPerProduct[product.id] || 0) + 1;
        const currentProductCount = this.totalCountPerProduct[product.id]
    }
}

this is my code but it is not working

Here, check this.

Yes, but can you talk to us about how you are stuck and what you are thinking instead of just asking us to fix it for you?

1 Like

so i have tried this

    constructor() {
        this.items = [];
        this.totalCountPerProduct = {}; 
    }

    addItem(id, products) {
        const product = products.find((item) => item.id === id);

        if (product) {
           
            if (!this.items.includes(product)) {
                this.items.push(product);
            }
            
           
            this.totalCountPerProduct[product.id] = 
                (this.totalCountPerProduct[product.id] || 0) + 1;

          
            const currentProductCount = this.totalCountPerProduct[product.id];

            return currentProductCount;  
        }

        return false; 
    }
}

what am i missing?

Can you talk to us about how you are stuck and what you are thinking instead of just asking us to fix it for you?

i am stuck because i am not sure if i am missing something

If the tests are not passing, then yes, you are missing something.

what am i missing here?

    constructor() {
        this.items = [];
        this.totalCountPerProduct = {}; 
    }

    addItem(id, products) {
        const product = products.find((item) => item.id === id);

        if (product) {
           
            if (!this.items.includes(product)) {
                this.items.push(product);
            }
            
           
            this.totalCountPerProduct[product.id] = 
                (this.totalCountPerProduct[product.id] || 0) + 1;

          
            const currentProductCount = this.totalCountPerProduct[product.id];

            return currentProductCount;  
        }

        return false; 
    }
}

I cannot write the solution for you.

It is much, much better if you try to talk about how you are stuck figuring out what’s wrong.

Please try to talk about how you are stuck.

Being able to talk about code is a critical job ready skill and you cannot find work as a programmer without it, but its a hard skill that requires practice. Without trying to talk about code, you really are holding your own learning back.

Something like “I created this variable here that the instructions were talking about, and tried to set it to the value they requested, but it doesn’t seem to be correct” would be a great place to start.

Hi @mbas

Here is a comparison of the original code and your code.

The code in blue is the original code, the code in red is your code.
The code in magenta is the overlap.

You appear to have removed and inserted code which you were not asked to do.
Please reset the step to restore to seed code.

For this step you need to write just one assignment.

Happy coding