-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce.js
More file actions
32 lines (23 loc) · 889 Bytes
/
Copy pathreduce.js
File metadata and controls
32 lines (23 loc) · 889 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
const myNums = [1, 2, 3];
const myTotal = myNums.reduce(function (acc, currval) {
console.log(`acc: ${acc} and currval: ${currval}`);
return acc + currval;
}, 0); // Initial value set to 0
console.log(myTotal);
const myNum = [1, 2, 3];
const myTota = myNum.reduce((acc, currval) => {
console.log(`acc: ${acc} and currval: ${currval}`);
return acc + currval;
}, 0); // Initial value set to 0
console.log(myTota);
const shoppingCart = [
{ item: "Laptop", price: 1000 },
{ item: "Phone", price: 500 },
{ item: "Headphones", price: 100 },
{ item: "Mouse", price: 50 }
];
const totalPrice = shoppingCart.reduce((acc, currItem) => {
console.log(`acc: $${acc} and adding item: ${currItem.item} with price: $${currItem.price}`);
return acc + currItem.price;
}, 0); // Initial value set to 0
console.log(`Total Price: $${totalPrice}`);