15.6 Homework Assignment
Task:
Example Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array Operations Homework</title>
</head>
<body>
<h1>Check the Console for Array Outputs</h1>
<script>
// Step 1: Create an array of numbers
let numbers = [10, 5, 8, 3, 7];
// Step 2: Add a number to the array using push()
numbers.push(12);
console.log("After adding 12: " + numbers); // Outputs: [10, 5, 8, 3, 7, 12]
// Step 3: Remove a number from the array using splice() (removes the second element)
numbers.splice(1, 1); // Removes number at index 1 (5)
console.log("After removing the second number: " + numbers); // Outputs: [10, 8, 3, 7, 12]
// Step 4: Sort the array in ascending order using sort()
numbers.sort((a, b) => a - b); // Sorts in ascending order
console.log("After sorting: " + numbers); // Outputs: [3, 7, 8, 10, 12]
// Step 5: Reverse the order of the array using reverse()
numbers.reverse();
console.log("After reversing: " + numbers); // Outputs: [12, 10, 8, 7, 3]
</script>
</body>
</html>Instructions:
Challenge:
Homework Submission:
Expected Outcome:
Last updated