|
| 1 | +/* Create a system that processes student data, calculates grades, filters results, and generates reports. It should handle real-world logic like grade calculations, filtering by criteria, and data transformations. |
| 2 | +
|
| 3 | +What to Build: |
| 4 | +An array of student objects, where each student has: name, marks (array of subject scores), attendance percentage |
| 5 | +Functions that: |
| 6 | +Calculate average marks and assign grades (A/B/C/D based on ranges) |
| 7 | +Filter students by criteria (e.g., "show only students with > 75 average") |
| 8 | +Find top performer and lowest scorer |
| 9 | +Check eligibility (average > 40 AND attendance > 75%) |
| 10 | +Loops to iterate through data and transform it |
| 11 | +Conditionals to apply grading rules (use switch for grade assignment or if/else for eligibility) |
| 12 | +Generate a summary report showing: total students, pass count, fail count, average class score |
| 13 | +How to Approach It: |
| 14 | +Start with data structure — Create realistic student objects with nested arrays (marks per subject) |
| 15 | +Build utility functions — Each function should do ONE thing (calculate average, assign grade, filter students) |
| 16 | +Use loops strategically — .map() mindset (though not required): transform data from one form to another |
| 17 | +Apply game logic — Use conditionals creatively (e.g., bonus points if attendance > 90%) |
| 18 | +Combine everything — Chain functions and loops to build the final report |
| 19 | +Challenge Twist (Optional): |
| 20 | +Add a pass/fail rule: If marks < 40, fail regardless of attendance |
| 21 | +Add a merit scholarship checker: Show students eligible for scholarship (average > 80 AND attendance 100) |
| 22 | +Export data to a formatted string report using template literals |
| 23 | +This project forces them to design with objects, organize logic with functions, and process data with loops — all core pillars. |
| 24 | + */ |
| 25 | +// Sample student data |
| 26 | +const students = [ |
| 27 | + { name: "Ram", marks: [85, 90, 78], attendance: 92 }, |
| 28 | + { name: "Shyam", marks: [70, 75, 80], attendance: 85 }, |
| 29 | + { name: "Hari", marks: [60, 65, 58], attendance: 80 }, |
| 30 | + { name: "Gita", marks: [30, 35, 28], attendance: 70 }, |
| 31 | + { name: "Sita", marks: [95, 98, 92], attendance: 100 } |
| 32 | +]; |
| 33 | + |
| 34 | +// Function to calculate average marks and assign grades |
| 35 | +function calculateGrade(student) { |
| 36 | + const average = student.marks.reduce((sum, mark) => sum + mark, 0) / student.marks.length; |
| 37 | + let grade; |
| 38 | + if (average >= 90) { |
| 39 | + grade = 'A'; |
| 40 | + } else if (average >= 80) { |
| 41 | + grade = 'B'; |
| 42 | + } |
| 43 | + else if (average >= 70) { |
| 44 | + grade = 'C'; |
| 45 | + } else if (average >= 40) { |
| 46 | + grade = 'D'; |
| 47 | + } |
| 48 | + else { |
| 49 | + grade = 'F'; |
| 50 | + } |
| 51 | + return { ...student, average, grade }; |
| 52 | +} |
| 53 | + |
| 54 | +// Process students to calculate grades |
| 55 | +const processedStudents = students.map(calculateGrade); |
| 56 | + |
| 57 | +// Filter students with average > 75 |
| 58 | +const topStudents = processedStudents.filter(student => student.average > 75); |
| 59 | +console.log("Top Students:", topStudents); |
| 60 | + |
| 61 | +// Find top performer and lowest scorer |
| 62 | +const topPerformer = processedStudents.reduce((top, student) => student.average > top.average ? student : top, processedStudents[0]); |
| 63 | +const lowestScorer = processedStudents.reduce((low, student) => student.average < low.average ? student : low, processedStudents[0]); |
| 64 | +console.log("Top Performer:", topPerformer); |
| 65 | +console.log("Lowest Scorer:", lowestScorer); |
| 66 | + |
| 67 | +// Check eligibility |
| 68 | +const eligibleStudents = processedStudents.filter(student => student.average > 40 && student.attendance > 75); |
| 69 | +console.log("Eligible Students:", eligibleStudents); |
| 70 | + |
| 71 | +// Generate summary report |
| 72 | +const totalStudents = processedStudents.length; |
| 73 | +const passCount = processedStudents.filter(student => student.grade !== 'F').length; |
| 74 | +const failCount = totalStudents - passCount; |
| 75 | +const averageClassScore = processedStudents.reduce((sum, student) => sum + student.average, 0) / totalStudents; |
| 76 | +console.log("Summary Report:"); |
| 77 | +console.log("- Total Students:", totalStudents); |
| 78 | +console.log("- Pass Count:", passCount); |
| 79 | +console.log("- Fail Count:", failCount); |
| 80 | +console.log("- Average Class Score:", averageClassScore.toFixed(2)); |
| 81 | + |
0 commit comments