Skip to content

Commit ba8dca0

Browse files
authored
Update Object.md
1 parent 2afaea6 commit ba8dca0

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

Object.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,54 @@ bob grade is 100
267267
cindy grade is 75
268268
*/
269269
```
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+
const cartForParty = {
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+
function cashRegister(shoppingCart) {
298+
let totalPrice = 0;
299+
for (let [itemName, price] of Object.entries(shoppingCart)) {
300+
price = parseFloat(price);
301+
totalPrice += price;
302+
}
303+
return totalPrice;
304+
}
305+
306+
const cartForParty = {
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}`);
317+
// The total price of the shopping cart is 60.55
318+
```
319+
320+
</details>

0 commit comments

Comments
 (0)