-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheight.js
More file actions
51 lines (35 loc) · 958 Bytes
/
eight.js
File metadata and controls
51 lines (35 loc) · 958 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Reduce Method--> reduces the array to a single value.
const myNums=[1,2,3]
const newNUm=myNums.reduce(function(acc,currval){
// console.log(`acc:${acc} and currval:${currval}`);
return acc+currval;
},0) //here passing 0 as the accumulator value
console.log(newNUm);
//using reduce with arrow function here, 3 is the accumulator
const total=myNums.reduce((acc,currval)=>acc+currval,3)
console.log(total);
const shoppingCart = [
{
itemName: "js course",
price: 2999
},
{
itemName: "py course",
price: 999
},
{
itemName: "mobile dev course",
price: 5999
},
{
itemName: "data science course",
price: 12999
},
]
// const prices=shoppingCart.reduce((acc,item)=>acc+item.price,0)
// calculate total price using for each loop
let totalPrice = 0;
shoppingCart.forEach(item => {
totalPrice += item.price;
});
console.log(totalPrice);