diff --git a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-basic-oop-by-building-a-shopping-cart/63efdbc22a0c56070beabed7.md b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-basic-oop-by-building-a-shopping-cart/63efdbc22a0c56070beabed7.md index f716938e19c..2fcec338124 100644 --- a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-basic-oop-by-building-a-shopping-cart/63efdbc22a0c56070beabed7.md +++ b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-basic-oop-by-building-a-shopping-cart/63efdbc22a0c56070beabed7.md @@ -7,7 +7,24 @@ dashedName: step-23 # --description-- -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. +You’re on the right track! However, let’s take a moment to address a common issue when working with objects in JavaScript. + +When you try to access an object property that doesn’t exist, JavaScript returns `undefined`. If you then attempt to perform arithmetic operations on `undefined`, it can lead to unexpected results, such as `NaN`. + +To prevent this, you can use the `||` (logical OR) operator to provide a default value. + +```js + let scores = {}; + let players = ["Alice", "Bob", "Charlie"]; + + players.forEach(player => { + scores[player] = scores[player] || 0; + }); +``` + +Now, let’s apply this concept to your `totalCountPerProduct` object in the `forEach` callback. Make sure that each `dessert.id` property is initialized properly. + +Initialize `totalCountPerProduct[dessert.id]` with a default value of `0` using the `||` operator. # --hints-- @@ -25,21 +42,13 @@ const cart = new ShoppingCart(); assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]/); ``` -You should use the assignment operator to update the value of the property of `totalCountPerProduct` that corresponds to `dessert.id`. +You should initialize `totalCountPerProduct[dessert.id]` with `0` as a default value using `||` operator at the end of the expression. ```js const cart = new ShoppingCart(); -assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=/); +assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=\s*totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*\|\|\s*0\s*/); ``` -You should update the value of `totalCountPerProduct` to be the current value plus one. - -```js -const cart = new ShoppingCart(); -assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=\s*totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*\+\s*1/); -``` - - # --seed-- ## --seed-contents-- diff --git a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-basic-oop-by-building-a-shopping-cart/63efe370bbfc4a08d500118e.md b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-basic-oop-by-building-a-shopping-cart/63efe370bbfc4a08d500118e.md index 2422c24fad7..f7efb0a8644 100644 --- a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-basic-oop-by-building-a-shopping-cart/63efe370bbfc4a08d500118e.md +++ b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-basic-oop-by-building-a-shopping-cart/63efe370bbfc4a08d500118e.md @@ -7,34 +7,32 @@ dashedName: step-24 # --description-- -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. +In the `forEach` callback, wrap the right-hand assignment `totalCountPerProduct[dessert.id] || 0` in parentheses `()` to ensure proper evaluation, then increment the value by one. # --hints-- -You should wrap your right-hand `totalCountPerProduct[dessert.id]` in parentheses. +You should use dot notation to access the `id` property of `dessert`. ```js const cart = new ShoppingCart(); -assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=\s*\(\s*totalCountPerProduct\s*\[\s*dessert\.id\s*\]/); +assert.match(cart.addItem.toString(), /dessert\.id/); ``` -You should use the `||` operator. +You should use bracket notation to access the property of `totalCountPerProduct` that corresponds to `dessert.id`. ```js const cart = new ShoppingCart(); -assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=\s*\(\s*totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*\|\|\s*/); +assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]/); ``` -You should use `0` as your fallback value. +You should use the assignment operator to update the value of the property of `totalCountPerProduct` that corresponds to `dessert.id`. ```js const cart = new ShoppingCart(); -assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=\s*\(\s*totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*\|\|\s*0\s*\)/); +assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=/); ``` -You should still add `1` to the value. +You should wrap `totalCountPerProduct[dessert.id] || 0` in parentheses `()` before incrementing it by one. ```js const cart = new ShoppingCart(); @@ -316,7 +314,7 @@ class ShoppingCart { const totalCountPerProduct = {}; this.items.forEach((dessert) => { - totalCountPerProduct[dessert.id] = totalCountPerProduct[dessert.id] + 1; + totalCountPerProduct[dessert.id] = totalCountPerProduct[dessert.id] || 0 }) } --fcc-editable-region-- diff --git a/curriculum/challenges/english/25-front-end-development/workshop-shopping-cart/63efdbc22a0c56070beabed7.md b/curriculum/challenges/english/25-front-end-development/workshop-shopping-cart/63efdbc22a0c56070beabed7.md index f716938e19c..2fcec338124 100644 --- a/curriculum/challenges/english/25-front-end-development/workshop-shopping-cart/63efdbc22a0c56070beabed7.md +++ b/curriculum/challenges/english/25-front-end-development/workshop-shopping-cart/63efdbc22a0c56070beabed7.md @@ -7,7 +7,24 @@ dashedName: step-23 # --description-- -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. +You’re on the right track! However, let’s take a moment to address a common issue when working with objects in JavaScript. + +When you try to access an object property that doesn’t exist, JavaScript returns `undefined`. If you then attempt to perform arithmetic operations on `undefined`, it can lead to unexpected results, such as `NaN`. + +To prevent this, you can use the `||` (logical OR) operator to provide a default value. + +```js + let scores = {}; + let players = ["Alice", "Bob", "Charlie"]; + + players.forEach(player => { + scores[player] = scores[player] || 0; + }); +``` + +Now, let’s apply this concept to your `totalCountPerProduct` object in the `forEach` callback. Make sure that each `dessert.id` property is initialized properly. + +Initialize `totalCountPerProduct[dessert.id]` with a default value of `0` using the `||` operator. # --hints-- @@ -25,21 +42,13 @@ const cart = new ShoppingCart(); assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]/); ``` -You should use the assignment operator to update the value of the property of `totalCountPerProduct` that corresponds to `dessert.id`. +You should initialize `totalCountPerProduct[dessert.id]` with `0` as a default value using `||` operator at the end of the expression. ```js const cart = new ShoppingCart(); -assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=/); +assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=\s*totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*\|\|\s*0\s*/); ``` -You should update the value of `totalCountPerProduct` to be the current value plus one. - -```js -const cart = new ShoppingCart(); -assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=\s*totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*\+\s*1/); -``` - - # --seed-- ## --seed-contents-- diff --git a/curriculum/challenges/english/25-front-end-development/workshop-shopping-cart/63efe370bbfc4a08d500118e.md b/curriculum/challenges/english/25-front-end-development/workshop-shopping-cart/63efe370bbfc4a08d500118e.md index 2422c24fad7..f7efb0a8644 100644 --- a/curriculum/challenges/english/25-front-end-development/workshop-shopping-cart/63efe370bbfc4a08d500118e.md +++ b/curriculum/challenges/english/25-front-end-development/workshop-shopping-cart/63efe370bbfc4a08d500118e.md @@ -7,34 +7,32 @@ dashedName: step-24 # --description-- -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. +In the `forEach` callback, wrap the right-hand assignment `totalCountPerProduct[dessert.id] || 0` in parentheses `()` to ensure proper evaluation, then increment the value by one. # --hints-- -You should wrap your right-hand `totalCountPerProduct[dessert.id]` in parentheses. +You should use dot notation to access the `id` property of `dessert`. ```js const cart = new ShoppingCart(); -assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=\s*\(\s*totalCountPerProduct\s*\[\s*dessert\.id\s*\]/); +assert.match(cart.addItem.toString(), /dessert\.id/); ``` -You should use the `||` operator. +You should use bracket notation to access the property of `totalCountPerProduct` that corresponds to `dessert.id`. ```js const cart = new ShoppingCart(); -assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=\s*\(\s*totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*\|\|\s*/); +assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]/); ``` -You should use `0` as your fallback value. +You should use the assignment operator to update the value of the property of `totalCountPerProduct` that corresponds to `dessert.id`. ```js const cart = new ShoppingCart(); -assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=\s*\(\s*totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*\|\|\s*0\s*\)/); +assert.match(cart.addItem.toString(), /totalCountPerProduct\s*\[\s*dessert\.id\s*\]\s*=/); ``` -You should still add `1` to the value. +You should wrap `totalCountPerProduct[dessert.id] || 0` in parentheses `()` before incrementing it by one. ```js const cart = new ShoppingCart(); @@ -316,7 +314,7 @@ class ShoppingCart { const totalCountPerProduct = {}; this.items.forEach((dessert) => { - totalCountPerProduct[dessert.id] = totalCountPerProduct[dessert.id] + 1; + totalCountPerProduct[dessert.id] = totalCountPerProduct[dessert.id] || 0 }) } --fcc-editable-region--