Learn Basic OOP by Building a Shopping Cart: Step 27

I am stuck here and could use some help. I feel like the code is correct and have tried different ways but I cannot get the check button to check my code and get a possible hint on why its not passing. Here is the step instructions as well as my current code.
Thank you!

The behaviour of the addItem method needs to change if the product is already in the cart or not. Create a ternary that checks if the current product is already in the cart. Use undefined for both the truthy and falsy expressions to avoid a syntax error.

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}`);
const isProductInCart = currentProductCountSpan ? true : false;
isProductInCart ? undefined : undefined;

};

Hello @chewmungadunga !

Step 27
The behaviour of the addItem method needs to change if the product is already in the cart or not. Create a ternary that checks if the current product is already in the cart. Use undefined for both the truthy and falsy expressions to avoid a syntax error.

Ok, so it’s the step above that is the issue. Essentially, it is asking for a ternary expression (e.g. some boolean expression ? action if true : action if false). You’ve got something that is very close to the expected answer, but it just needs to be written with the existing variables.

The best way forward will be to reset the challenge step and start step 27 again.

The currentProductCount variable has everything you need to work with.

Does this help?
Keep up the good progress!

Happy Coding! :slightly_smiling_face:

1 Like

Thank you so much for your help! I was missing a darn end } , go figure. :man_facepalming:

1 Like