Complete the code to traverse the array forward and print each element.
const arr = [1, 2, 3, 4, 5]; for (let i = 0; i [1] arr.length; i++) { console.log(arr[i]); }
The loop should run while i is less than the array length to avoid going out of bounds.
Complete the code to traverse the array backward and print each element except the first one.
const arr = [1, 2, 3, 4, 5]; for (let i = arr.length - 1; i [1] 0; i--) { console.log(arr[i]); }
The loop should continue while i is greater than 0 to include all elements except index 0.
Fix the error in the backward traversal loop condition to include the first element.
const arr = [10, 20, 30, 40]; for (let i = arr.length - 1; i [1] 0; i--) { console.log(arr[i]); }
Using >= 0 ensures the loop includes the element at index 0.
Fill both blanks to create a forward traversal that prints only even numbers.
const nums = [1, 2, 3, 4, 5, 6]; for (let i = 0; i [1] nums.length; i++) { if (nums[i] [2] 2 === 0) { console.log(nums[i]); } }
The loop runs while i is less than the array length. The modulus operator % checks if the number is even.
Fill all three blanks to create a backward traversal that prints numbers greater than 3.
const values = [2, 4, 6, 1, 5]; for (let i = values.length - 1; i [1] 0; i--) { if (values[i] [2] 3) { console.log(values[[3]]); } }
The loop continues while i is greater than or equal to 0. The condition checks if the value is greater than 3. The index i is used to access the array element.
