-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce_function.js
More file actions
31 lines (25 loc) · 779 Bytes
/
reduce_function.js
File metadata and controls
31 lines (25 loc) · 779 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
// cartItem array
const cartItem = [
{ id: 1, name: "Apple", price: 120, quantity: 2 },
{ id: 2, name: "Banana", price: 60, quantity: 5 },
{ id: 3, name: "Orange", price: 100, quantity: 3 },
];
const subTotal = cartItem.reduce((subtotal, product) => {
return subtotal + product.price * product.quantity;
}, 0);
// console.log(subTotal);
// name, score array
const students = [
{ name: "Rahim", score: 85 },
{ name: "Karim", score: 78 },
{ name: "Amina", score: 92 },
];
// get the best student
const bestStudent = students.reduce((bestStudent, students) => {
console.log(bestStudent, students);
if (bestStudent.score > students.score) {
return bestStudent;
}
return students;
}, students[0]);
console.log("Best student score is : ", bestStudent);