-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotal_savory.js
64 lines (58 loc) · 1.1 KB
/
total_savory.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
/*
Use reduce() and only reduce() to calculate and return
the total cost of only the savory
items in the shopping cart.
Expected output: 9.97
*/
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 totalSavory(arr) {
return arr.reduce((sum, { type, price }) => {
if (type === "savory") {
sum += price;
}
// else {
// sum += 0;
// }
return sum;
}, 0)
}
// const next = totalSavory(shoppingCart);
// console.log(next);
console.log(totalSavory(shoppingCart));