feat(curriculum): clarify shortcircuiting in shopping cart project (#59053)

This commit is contained in:
Sorya Ek
2025-03-05 11:54:08 -08:00
committed by GitHub
parent 90a2280d5b
commit bb835c026c
4 changed files with 58 additions and 44 deletions

View File

@@ -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.
Youre on the right track! However, lets take a moment to address a common issue when working with objects in JavaScript.
When you try to access an object property that doesnt 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, lets 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--

View File

@@ -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--

View File

@@ -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.
Youre on the right track! However, lets take a moment to address a common issue when working with objects in JavaScript.
When you try to access an object property that doesnt 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, lets 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--

View File

@@ -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--