├── index.html ├── script.js └── style.css /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Array of Object - handson 7 | 8 | 9 | 10 | 11 | 12 |

Array of Object - Handson

13 |
14 |
15 |
16 |
 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 |
23 |
24 |
25 |

Q1 :- We are interested in retrieving only the students' names; all the names should be in caps.
26 | ['JOHN', 'BABA', 'YAGA', 'WICK']

27 |

ANS:-

28 |
 29 |                 studentRecords.map((item)=>{
 30 |                     console.log(item.name.toUpperCase())
 31 |                 })
 32 |             
33 |

ANS :- ['JOHN', 'BABA', 'YAGA', 'WICK']

34 |
35 |
36 |
37 |

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 |

40 |

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 |

51 |
52 |
53 |
54 |

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 |
65 |
66 |
67 |

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 |
78 |
79 |
80 |

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

90 |
91 |
92 |
93 |

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 |
105 |
106 |
107 |

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 |

134 |
135 |
136 |
137 |

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 |
151 |
152 |
153 | 154 | 155 | -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | let studentRecords = [ 2 | { name: "John", id: 123, marks: 98 }, 3 | { name: "Baba", id: 101, marks: 23 }, 4 | { name: "yaga", id: 200, marks: 45 }, 5 | { name: "Wick", id: 115, marks: 75 }, 6 | ]; 7 | 8 | // Question :1 9 | studentRecords.map((item) => { 10 | console.log(item.name.toUpperCase()); 11 | }); 12 | 13 | // Question 2 14 | studentRecords.map((item) => { 15 | if (item.marks > 50) { 16 | console.log(item); 17 | } 18 | }); 19 | 20 | // Question 3 21 | studentRecords.map((item) => { 22 | if (item.marks > 50 && item.id > 120) { 23 | console.log(item); 24 | } 25 | }); 26 | 27 | // Question 4 28 | const sum = studentRecords.reduce((total, current) => { 29 | return total + current.marks; 30 | }, 0); 31 | console.log(sum); 32 | 33 | // Question 5 34 | studentRecords.map((item) => { 35 | if (item.marks > 50) { 36 | console.log(item.name); 37 | } 38 | }); 39 | 40 | // Question 6 41 | 42 | const sum2 = studentRecords 43 | .filter((item) => { 44 | return item.id > 120; 45 | }) 46 | .reduce((total, current) => { 47 | return total + current.marks; 48 | }, 0); 49 | 50 | console.log(sum2); 51 | 52 | // Question 7 53 | 54 | function calculateTotalMarksWithGrace(student) { 55 | if (student.marks < 50) { 56 | // Add 15 grace marks if the student's marks are less than 50 57 | return student.marks + 15; 58 | } else { 59 | return student.marks; 60 | } 61 | } 62 | 63 | // Calculate and print total marks for students with marks greater than 50 after applying grace 64 | for (let i = 0; i < studentRecords.length; i++) { 65 | let student = studentRecords[i]; 66 | let totalMarks = calculateTotalMarksWithGrace(student); 67 | 68 | if (totalMarks > 50) { 69 | console.log(`Student: ${student.name}, Total Marks: ${totalMarks}`); 70 | } 71 | } 72 | 73 | // Question 8 74 | let student = [ 75 | { name: 'John', class: '10th', rollNo: 1 }, 76 | { name: 'Jane', class: '9th', rollNo: 2 }, 77 | { name: 'Alice', class: '12th', rollNo: 3 }, 78 | { name: 'Bob', class: '11th', rollNo: 4 }, 79 | { name: 'Sarah', class: '8th', rollNo: 5 }, 80 | { name: 'Mike', class: '10th', rollNo: 6 } 81 | ]; 82 | 83 | console.log(student); 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | *{ 2 | margin: 0; 3 | padding: 0; 4 | } 5 | h1{ 6 | color: red; 7 | text-align: center; 8 | font-size: 40px; 9 | text-decoration: underline; 10 | font-weight: 900; 11 | margin-top: 20px; 12 | } 13 | hr{ 14 | width: 100%; 15 | height: 3px; 16 | background-color: grey; 17 | margin: 20px 0; 18 | } 19 | .mainContanier{ 20 | margin: 0 60px; 21 | } 22 | .mainquestion{ 23 | font-weight: 600; 24 | font-size: 20px; 25 | text-align: center; 26 | } 27 | .bold{ 28 | font-weight: 600; 29 | } 30 | .blue{ 31 | color: blue; 32 | } 33 | --------------------------------------------------------------------------------