You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Object.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -267,3 +267,54 @@ bob grade is 100
267
267
cindy grade is 75
268
268
*/
269
269
```
270
+
271
+
## Challenges
272
+
273
+
#### Challenge 1.
274
+
275
+
Write a function called cashRegister that takes a shopping cart object. The object contains item names and prices (itemName: itemPrice). The function should return the total price of the shopping cart.
276
+
277
+
```javascript
278
+
Example
279
+
// Input
280
+
constcartForParty= {
281
+
banana:'1.25',
282
+
handkerchief:'.99',
283
+
tShirt:'25.01',
284
+
apple:'0.60',
285
+
nalgene:'10.34',
286
+
proteinShake:'22.36'
287
+
};
288
+
289
+
// Output
290
+
cashRegister(cartForParty)); // 60.55
291
+
```
292
+
293
+
<details>
294
+
<summary>Solution</summary>
295
+
296
+
```javascript
297
+
functioncashRegister(shoppingCart) {
298
+
let totalPrice =0;
299
+
for (let [itemName, price] ofObject.entries(shoppingCart)) {
300
+
price =parseFloat(price);
301
+
totalPrice += price;
302
+
}
303
+
return totalPrice;
304
+
}
305
+
306
+
constcartForParty= {
307
+
banana:'1.25',
308
+
handkerchief:'.99',
309
+
tShirt:'25.01',
310
+
apple:'0.60',
311
+
nalgene:'10.34',
312
+
proteinShake:'22.36'
313
+
};
314
+
315
+
let totalPrice =cashRegister(cartForParty);
316
+
console.log(`The total price of the shopping cart is ${totalPrice}`);
0 commit comments