Mental Model
Going through each item in an array one by one to look at or change it.
Analogy: Like reading a list of names on a paper from top to bottom, checking each name carefully.
[0] -> [1] -> [2] -> [3] -> null ↑ start at first element
[0] -> [1] -> [2] -> [3] -> null ↑ start at first element
[0:5↑] -> [1:10] -> [2:15] -> [3:20] -> null
[0:5] -> [1:10↑] -> [2:15] -> [3:20] -> null
[0:5] -> [1:10] -> [2:15↑] -> [3:20] -> null
[0:5] -> [1:10] -> [2:15] -> [3:20↑] -> null
[0:5] -> [1:10] -> [2:15] -> [3:20] -> null Printed: 5 10 15 20
class ArrayTraversal { static printElements(arr) { for (let i = 0; i < arr.length; i++) { console.log(arr[i]); // print current element } } } const inputArray = [5, 10, 15, 20]; ArrayTraversal.printElements(inputArray);
for (let i = 0; i < arr.length; i++) {console.log(arr[i]); // print current elementfor (let i = 0; i < arr.length; i++) {
for (let i = 0; i < arr.length; i++) {
for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }arr using a for...of loop in JavaScript?for...of syntaxfor...of loop iterates over values directly, so syntax is for (let element of arr).for...of syntax. Others use different loops or incorrect conditions.for...of loops over values [OK]const arr = [10, 20, 30];
let result = '';
for (let i = arr.length - 1; i >= 0; i--) {
result += arr[i] + ' ';
}
console.log(result.trim());arr:const arr = [1, 2, 3];
for (let i = 0; i <= arr.length; i++) {
console.log(arr[i]);
}arr = [[1, 2], [3, 4], [5, 6]], which code correctly prints all elements in order using nested loops?Pass the test and this pattern moves unknown