-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq01.js
135 lines (119 loc) · 2.83 KB
/
q01.js
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
124
125
126
127
128
129
130
131
132
133
134
135
// Given an array of objects of student's marks:
const studentDetails = [
{
roll: "1",
name: "Rohan Singh",
maths: 86,
science: 90,
english: 75,
computer: 85
},
{
roll: "2",
name: "Ritvik Patel",
maths: 27,
science: 30,
english: 35,
computer: 30
},
{
roll: "3",
name: "Neha Maurya",
maths: 75,
science: 69,
english: 40,
computer: 75
},
{
roll: "4",
name: "Mohit Verma",
maths: 21,
science: 31,
english: 45,
computer: 40
},
{
roll: "5",
name: "Karan Trivedi",
maths: 70,
science: 80,
english: 35,
computer: 60
}
];
// Q1. Print the name and total marks of each student.
const funOne = arr => {
let resultArr = [];
for (obj of arr) {
const studentName = obj.name;
const totalMarks = obj.english + obj.maths + obj.science + obj.computer;
resultArr.push({studentName, totalMarks});
}
return resultArr;
}
// console.table(funOne(studentDetails))
// Q2. Print the name of student whose total marks is highest.
const funTwo = arr => {
let maxTotalMarks = 0;
let name;
for (obj of arr) {
const totalMarks = obj.english + obj.maths + obj.science + obj.computer;
if (totalMarks > maxTotalMarks) {
maxTotalMarks = totalMarks;
name = obj.name;
}
}
return name;
}
// Q3. Print the name of student whose total marks is lowest.
const funThree = arr => {
let minTotalMarks = Infinity;
let name;
for (obj of arr) {
const totalMarks = obj.english + obj.maths + obj.science + obj.computer;
if (totalMarks < minTotalMarks) {
minTotalMarks = totalMarks;
name = obj.name;
}
}
return name;
}
// Q4. Print the average marks of the class in computer subject.
const funFour = arr => {
let computerMarks = 0;
for (obj of arr) {
computerMarks += obj.computer
}
return computerMarks/arr.length
}
// Q5. Print the grades of all students:
// -> Grade A if total marks is higher than or equal to 75%,
// -> Grade B if higher than or equal to 60%,
// -> Grade C if higher than or equal to 35%,
// -> Grade D if lower than 35%.
const funFive = arr => {
const resultArr = [];
for (student of studentDetails) {
const totalMarks = student.english + student.maths + student.science + student.computer;
const name = student.name;
const studentPercentage = (totalMarks/400)*100;
if (studentPercentage >= 75) {
resultArr.push({name, Grade: "A"})
} else if (studentPercentage >= 60) {
resultArr.push({name, Grade: "B"})
} else if (studentPercentage >= 35) {
resultArr.push({name, Grade: "C"})
} else {
resultArr.push({name, Grade: "D"})
}
}
return resultArr;
}
// console.table(funFive(studentDetails))
// Q6. Print the total number of students passed and their names. Pass when total marks is greater than 35%.
const funSix = arr => {
const temp = funFive(arr);
return temp.filter(student => student.Grade !== "D" )
}
// Call and test functions here:
console.log();