-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshopping_cart.js
72 lines (59 loc) · 1.22 KB
/
shopping_cart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Use reduce() to total the groceries.
Then find a method that will round the total to 2 decimal places.
Example output: 73.44
*/
const shoppingCart = [
{
item: "🍭",
price: 2.99,
type: "sweet",
onSale: true
},
{
item: "🍫",
price: 1.99,
type: "sweet",
onSale: true
},
{
item: "🥫",
price: 1.99,
type: "savory",
onSale: false
},
{
item: "🍬",
price: .89,
type: "sweet",
onSale: false
},
{
item: "🥦",
price: 3.99,
type: "savory",
onSale: false
},
{
item: "🍖",
price: 3.99,
type: "savory",
onSale: true
},
]
function total(arr) {
return arr.reduce((sum, { price }) => {
sum += price;
return sum;
}, 0).toFixed(2); // .toFixed(0); --> rounds off the number to the upper bound
}
// arr.reduce((acc, curr) => {
// }, 0)
console.log(total(shoppingCart));
// function total(arr) {
// const total = arr.reduce((acc, curr) => {
// return acc + curr.price;
// }, 0);
// return total.toFixed(2);
// }
// console.log(total(shoppingCart));