-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce.js
More file actions
52 lines (42 loc) · 1.57 KB
/
Copy pathreduce.js
File metadata and controls
52 lines (42 loc) · 1.57 KB
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
50
51
52
// strings = ["Hello"," ","world","!"];
// //how can you use the reduce() function to concatenate strings in an array
// // const strings = ["Hello", " ", "world", "!"];
// const result = strings.reduce((accumulator, currentValue) => {
// return accumulator + currentValue;
// });
// console.log(result);
// //let arr = [23,45,66,72,16];
// //arr.reduce(() >= )
// let arr = [
// {firstname:'rahul',lastname:'jhA',age:25},
// {firstname:'donald',lastname:'trump',age:76},
// {firstname:'vikas',lastname:'xyz',age:20},
// {firstname:'depika',lastname:'paudukone',age:25}
// ];
// //using reduce() to count how many people are of each age
// const ageCount = arr.reduce((acc, person) => {
// if (acc[person.age]) {
// acc[person.age]++;
// }
// else {
// acc[person.age] = 1;
// }
// return acc;
// }, {});
// console.log(ageCount);
// print total marks for students with marks greater than 60 after 20 marks have been added to those who scored less than 60.
let students = [
{name:"smith",rollno:31,marks:80},
{name:"jenny",rollno:15,marks:69},
{name:"john",rollno:16,marks:35},
{name:"tiger",rollno:17,marks:55},
]
let updatedStudents = students.map(students => {
if (students.marks < 60) {
students.marks += 20;
}
return students;
});
let passedStudents = updatedStudents.filter(student => student.marks > 60);
let totalMarks = passedStudents.reduce((acc, student) => acc + student.marks, 0);
console.log("Total Marks of students with >60:", totalMarks);