-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-10.html
More file actions
123 lines (102 loc) · 4.83 KB
/
index-10.html
File metadata and controls
123 lines (102 loc) · 4.83 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="keywords" content="Assignment">
<title>Coding Assisement</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<h2>10. Array of objects</h2>
<!-- ====== write code -->
<div class="Que">
<p>You have given:</p>
<pre style="color: rgb(19, 1, 85);">
let studentRecords = [ {name: 'John', id: 123, marks : 98 },
{name: 'Baba', id: 101, marks : 23 },
{name: 'yaga', id: 200, marks : 45 },
{name: 'Wick', id: 115, marks : 75 }
];
</pre>
<p>Que.1 We are interested in retrieving only the students' names; all the names should be in caps. <br>
<br> ['JOHN', 'BABA', 'YAGA', 'WICK']</p>
<pre>
let studentNames = studentRecords.map(student => student.name.toUpperCase());
console.log(studentNames); // ['JOHN', 'BABA', 'YAGA', 'WICK']
</pre>
<p>Que.2 Suppose we have the same dataset as above but this time we
want to get the details of students who scored more than 50 marks. <br>
<br> [{name: 'John', id: 123, marks: 98 },
{name: 'Wick', id: 115, marks: 75 }]</p>
<pre>
let highScorers = studentRecords.filter((student) => student.marks > 50);
console.log(highScorers); // [{name: 'John', id: 123, marks: 98 },{name: 'Wick', id: 115, marks: 75 }]
</pre>
<p>Que.3 Retrieve the details of students who scored more than 50 marks and have IDs greater than 120.</p>
<pre>
let filteredStudents = studentRecords.filter(
(student) => student.marks > 50 && student.id > 120
);
console.log(filteredStudents); // [{name: 'John', id: 123, marks: 98 }]
</pre>
<p>Que.4 Consider the same scenario we have discussed above, but this time we would like to know the sum total of the marks of the students.</p>
<pre>
let totalMarks = studentRecords.reduce(
(total, student) => total + student.marks, 0 );
console.log(totalMarks); // 241
</pre>
<p>Que.5 This time we want to get only the names of the students who scored more than 50 marks from the same dataset used above.</p>
<pre>
let highScorersNames = studentRecords.filter((student) => student.marks > 50).map((student) => student.name);
console.log(highScorersNames); // ['John', 'Wick']
</pre>
<p>Que.6 This time we are required to print the sum of marks of the students with id > 120.</p>
<pre>
let totalMarksIdGreaterThan120 = studentRecords.filter((student) => student.id > 120).reduce((total, student) => total + student.marks, 0);
console.log(totalMarksIdGreaterThan120); // 98 + 45 = 143
</pre>
<p>Que.7 This time we are required to print the total marks of the students with marks greater than 50 after a grace of 15 marks has been added to those students who scored less than 50.</p>
<pre>
let updatedMarks = studentRecords.map((student) => {
if (student.marks < 50) {
student.marks += 15;
}
return student;
// updatedMarks = [
// { name: "John", id: 123, marks: 98 },
// { name: "Baba", id: 101, marks: 38 },
// { name: "yaga", id: 200, marks: 60 },
// { name: "Wick", id: 115, marks: 75 },
// ];
});
let highScorersWithUpdatedMarks = updatedMarks.filter((student) => student.marks > 50);
// highScorersWithUpdatedMarks = [
// { name: "John", id: 123, marks: 98 },
// { name: "yaga", id: 200, marks: 60 },
// { name: "Wick", id: 115, marks: 75 },
// ];
let totalMarksAfterGrace = highScorersWithUpdatedMarks.reduce((total, student) => total + student.marks, 0);
// totalMarksAfterGrace = 98 + 60 + 75 = 233
console.log(totalMarksAfterGrace); // 233
</pre>
<p>Que.8 Create 6 objects , each object will have name, class, roll no as properties. Store these objects in an array of objects. </p>
<pre>
let students = [
{ name: "John", class: "A", rollNo: 1 },
{ name: "Baba", class: "B", rollNo: 2 },
{ name: "Yaga", class: "A", rollNo: 3 },
{ name: "Wick", class: "B", rollNo: 4 },
{ name: "Alice", class: "A", rollNo: 5 },
{ name: "Bob", class: "B", rollNo: 6 },
];
console.log(students); // [{name: 'John', class: 'A', rollNo: 1 }, {name: 'Baba', class: 'B', rollNo: 2 }, {name: 'Yaga', class: 'A', rollNo: 3 }, {name: 'Wick', class: 'B', rollNo: 4 }, {name: 'Alice', class: 'A', rollNo: 5 }, {name: 'Bob', class: 'B
</pre>
</div>
<div class="container">
<div class="row"><a href="./index-9.html">Back</a></div>
<div class="row"><a href="./index-11.html">Next</a></div>
</div>
</body>
</html>