17 | let studentRecords = [ {name: 'John', id: 123, marks : 98 }, 18 | {name: 'Baba', id: 101, marks : 23 }, 19 | {name: 'yaga', id: 200, marks : 45 }, 20 | {name: 'Wick', id: 115, marks : 75 } ] 21 |22 |
24 |
Q1 :- We are interested in retrieving only the students' names; all the names should be in caps.
26 | ['JOHN', 'BABA', 'YAGA', 'WICK']
ANS:-
28 |29 | studentRecords.map((item)=>{ 30 | console.log(item.name.toUpperCase()) 31 | }) 32 |33 |
ANS :- ['JOHN', 'BABA', 'YAGA', 'WICK']
34 |36 |
Q2 :- 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.
38 | [{name: 'John', id: 123, marks: 98 },{name: 'Wick', id: 115, marks: 75 }]
39 |
ANS :-
41 |42 | studentRecords.map((item)=>{ 43 | if(item.marks > 50){ 44 | console.log(item) 45 | } 46 | }) 47 |48 |
OUTPUT :- {name: 'John', id: 123, marks: 98}
49 | {name: 'Wick', id: 115, marks: 75}
50 |
53 |
Q3 :- Retrieve the details of students who scored more than 50 marks and have IDs greater than 120.
55 |ANS :-
56 |57 | studentRecords.map((item) => { 58 | if (item.marks > 50 && item.id > 120) { 59 | console.log(item); 60 | } 61 | }); 62 |63 |
OUTPUT :- { name: 'John', id: 123, marks: 98 }
64 |66 |
Q4 :- 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.
68 |ANS :-
69 |70 | const sum = studentRecords.reduce((total, current) => { 71 | return total + current.marks; 72 | 73 | }, 0); 74 | console.log(sum); 75 |76 |
OUTPUT :- 241
77 |79 |
Q5 :- This time we want to get only the names of the students who scored more than 50 marks from the same dataset used above.
81 |ANS :-
82 |83 | studentRecords.map((item) => { 84 | if (item.marks > 50) { 85 | console.log(item.name); 86 | } 87 | }); 88 |89 |
OUTPUT :- John
Wick
92 |
Q6 :- This time we are required to print the sum of marks of the students with id > 120.
94 |ANS :-
95 |96 | const sum2 = studentRecords.filter((item) => { 97 | return item.id > 120; 98 | }).reduce((total, current) => { 99 | return total + current.marks; 100 | }, 0); 101 | console.log(sum2); 102 |103 |
OUTPUT :- 143
104 |106 |
Q7 :- 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.
108 |ANS :-
109 |110 | function calculateTotalMarksWithGrace(student) { 111 | if (student.marks < 50) { 112 | // Add 15 grace marks if the student's marks are less than 50 113 | return student.marks + 15; 114 | } else { 115 | return student.marks; 116 | } 117 | } 118 | 119 | // Calculate and print total marks for students with marks greater than 50 after applying grace 120 | for (let i = 0; i < studentRecords.length; i++) { 121 | let student = studentRecords[i]; 122 | let totalMarks = calculateTotalMarksWithGrace(student); 123 | 124 | if (totalMarks > 50) { 125 | console.log(`Student: ${student.name}, Total Marks: ${totalMarks}`); 126 | } 127 | } 128 |129 |
OUTPUT :-
130 | Student: John, Total Marks: 98
131 | Student: yaga, Total Marks: 60
132 | Student: Wick, Total Marks: 75
133 |
136 |
Q8 :- Create 6 objects , each object will have name, class, roll no as properties. Store these objects in an array of objects.
138 |ANS :-
139 |140 | let student = [ 141 | { name: 'John', class: '10th', rollNo: 1 }, 142 | { name: 'Jane', class: '9th', rollNo: 2 }, 143 | { name: 'Alice', class: '12th', rollNo: 3 }, 144 | { name: 'Bob', class: '11th', rollNo: 4 }, 145 | { name: 'Sarah', class: '8th', rollNo: 5 }, 146 | { name: 'Mike', class: '10th', rollNo: 6 } 147 | ]; 148 | console.log(student); 149 |150 |
152 |