-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path59.js
More file actions
34 lines (26 loc) · 972 Bytes
/
59.js
File metadata and controls
34 lines (26 loc) · 972 Bytes
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
// reduce
const numbers = [1,2,3,4,5, 10];
// aim : sum of all the numbers in array
// const sum = numbers.reduce((accumulator, currentValue)=>{
// return accumulator + currentValue;
// }, 100);
// console.log(sum);
// accumulator , currentValue, return
// 1 2 3
// 3 3 6
// 6 4 10
// 10 5 15
// 15 10 25
const userCart = [
{productId: 1, productName: "mobile", price: 12000},
{productId: 2, productName: "laptop", price: 22000},
{productId: 3, productName: "tv", price: 15000},
]
const totalAmount = userCart.reduce((totalPrice, currentProduct)=>{
return totalPrice + currentProduct.price;
}, 0)
console.log(totalAmount);
// total price currentValue return
// 0 {} 12000
// 12000 22000 34000
// 34000 15000 49000