-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheight_reduce.js
47 lines (38 loc) · 1.02 KB
/
eight_reduce.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
// Reduce
const myNums = [1, 2, 3]
const total = myNums.reduce( function (acc, currval) {
console.log(`acc: ${acc} and currVal: ${currval}`);
return acc + currval
}, 0 /* acc start value */ )
console.log(total);
/* Output:
acc: 0 and currval: 1 (0 + 1)
acc: 1 and currval: 2 (1 + 2)
acc: 3 and currval: 3 (3 + 3)
6 (total)
--------------------------------------------------------------------
'Reduce' in Arrow function */
const myTotal = myNums.reduce( (acc, curr) => acc + curr, 0 )
console.log(myTotal); /* Output: 6
--------------------------------------------------------------------
Real life example */
const shopppingCart = [
{
itemName: "JS Course",
price: 2999
},
{
itemName: "Py Course",
price: 999
},
{
itemName: "Mobile dev Course",
price: 5999
},
{
itemName: "Data Science Course",
price: 12999
},
]
const priceToPay = shopppingCart.reduce( (acc, item) => acc + item.price, 0 )
console.log(priceToPay); // Output: 22996