Exercise: Student List Management
Focus: Practice variables, String templates, functions, and callbacks without a UI.
Part 1: Variable Setup
• Create an array student to store student objects.
• Each student has:
o id (auto-incremented integer),
o name (string),
o scores (array of numbers).
• Expected: Array initialized with at least 3 students (e.g., [{ id: 1, name: "An", scores:
[8, 9, 7] }, ...]).
Part 2: String Template
• Write a function displayStudent that takes a student object and returns a formatted
string.
• Expected output:
Student: [name]
Scores: [list of scores]
Average: [average score, rounded to 1 decimal]
Example:
Student: An
Scores: 8, 9, 7
Average: 8.0
Part 3: Basic Functions
1. Function calculateAverage:
o Input: Array of scores.
o Expected: Returns average score (e.g., calculateAverage([8, 9, 7]) returns
8.0).
2. Function addStudent:
o Inputs: name (string), scores (array).
o Expected: Adds a new student to students with an auto-incremented id and
returns the new student object.
Part 4: Callbacks
1. Function findStudent:
o Inputs: id (number), callback function.
o Expected: Calls the callback with the found student object or null if not
found.
Example:
findStudent(2, (student) => console.log(student ? student.name : "Not found"));
// Output: "Bình" or "Not found"
2. Function filterStudents:
o Input: Callback condition.
o Expected: Returns an array of students meeting the condition (e.g., average
score >= 8).
Part 5: Test Program
• Expected:
1. Add a new student (e.g., { name: "Dung", scores: [7, 8, 6] }).
2. Display the new student’s info using displayStudent.
3. Find and print info of student with id = 1 using findStudent.
4. Filter and print students with average score >= 8 using filterStudents.
Notes
• Use console.log to verify outputs.
• No UI required—just focus on logic.
• Test all functions to ensure they work as expected.