15.4 Programming Activity (20 mins)
Step-by-Step Activity:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Student Names Array</title> </head> <body> <h1>Check the Console for Array Outputs</h1> <script> // Create an initial array of student names let students = ["Alice", "Bob", "Charlie", "Akyl", "Adilet", "Asan"]; // Function to add a student to the array function addStudent(name) { students.push(name); console.log("After adding " + name + ": " + students); } // Function to remove a student from the array function removeStudent(name) { let index = students.indexOf(name); if (index !== -1) { students.splice(index, 1); // Remove student if found console.log("After removing " + name + ": " + students); } else { console.log(name + " not found in the array."); } } // Function to sort the array of students alphabetically function sortStudents() { students.sort(); console.log("After sorting: " + students); } // Call functions to manipulate the array //--------------------------------------------- //modify the code for challenge 1 & 2 addStudent("Aisulu"); // Add a student removeStudent("Bob"); // Remove a student //--------------------------------------------- sortStudents(); // Sort the students array // Challenge: Use forEach() to iterate through the array and print names with formatting console.log("List of students:"); students.forEach(function(name, index) { console.log((index + 1) + ". " + name); // Print with serial number }); </script> </body> </html>
Explanation:
Run the file in a browser:
Challenge:
Activity Follow-up Questions:
Expected Outcome:
Last updated