Arrays and Iteration
Indexed Loop
Loop with an index to scan an array.
indexed-loop
A counter-based `for` loop exposes the index, which is handy when the position matters, such as scanning for the largest value.
Indexed Loop
indexed_loop.js
Replay: real traced execution (multi-file project)
const nums = [3, 7, 2, 8, 5];
const start = 0;
let max = nums[start];
for (let i = start; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
console.log("start=" + start);
console.log("max=" + max);
const nums = [3, 7, 2, 8, 5];
const start = 1;
let max = nums[start];
for (let i = start; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
console.log("start=" + start);
console.log("max=" + max);
const nums = [3, 7, 2, 8, 5];
const start = 2;
let max = nums[start];
for (let i = start; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
console.log("start=" + start);
console.log("max=" + max);
nums ← 3,7,2,8,5, start ← 0, max ← 3, nums[start] ← 3
1const nums→ 3,7,2,8,5 = [3, 7, 2, 8, 5];2const start→ 0 = 0; //@start=1, 23let max→ 3 = nums[start]→ 3;4for (let i = start; i < nums.length; i++) {for (let i = start; i < nums.length; i++)
pass 1 of 52const start = 0; //@start=1, 23let max = nums[start];4for (let i0 = start0; i < nums.length5; i++) {5 if (nums[i] > max) {All 5 passes — pass 1 is the card above pass inums[i]max1 0 — — 2 1 7 3 3 2 — — 4 3 8 7 5 4 — — if (nums[i] > max)
pass 1 of 23let max = nums[start];4for (let i = start; i < nums.length; i++) {5 if (nums[i]7 > max3) {6 max = nums[i]7;7 }if (nums[i] > max)
pass 2 of 23let max = nums[start];4for (let i = start; i < nums.length; i++) {5 if (nums[i]8 > max7) {6 max = nums[i]8;7 }console.log("start=" + start);
7 }8}910console.log("start=" + start0);11console.log("max=" + max8);outputstart=0 max=8
nums ← 3,7,2,8,5, start ← 1, max ← 7, nums[start] ← 7
1const nums→ 3,7,2,8,5 = [3, 7, 2, 8, 5];2const start→ 1 = 1;3let max→ 7 = nums[start]→ 7;4for (let i = start; i < nums.length; i++) {for (let i = start; i < nums.length; i++)
pass 1 of 42const start = 1;3let max = nums[start];4for (let i1 = start1; i < nums.length5; i++) {5 if (nums[i] > max) {All 4 passes — pass 1 is the card above pass inums[i]max1 1 — — 2 2 — — 3 3 8 7 4 4 — — if (nums[i] > max)
3let max = nums[start];4for (let i = start; i < nums.length; i++) {5 if (nums[i]8 > max7) {6 max = nums[i]8;7 }console.log("start=" + start);
7 }8}910console.log("start=" + start1);11console.log("max=" + max8);outputstart=1 max=8
nums ← 3,7,2,8,5, start ← 2, max ← 2, nums[start] ← 2
1const nums→ 3,7,2,8,5 = [3, 7, 2, 8, 5];2const start→ 2 = 2;3let max→ 2 = nums[start]→ 2;4for (let i = start; i < nums.length; i++) {for (let i = start; i < nums.length; i++)
pass 1 of 32const start = 2;3let max = nums[start];4for (let i2 = start2; i < nums.length5; i++) {5 if (nums[i] > max) {All 3 passes — pass 1 is the card above pass inums[i]max1 2 — — 2 3 8 2 3 4 — — if (nums[i] > max)
3let max = nums[start];4for (let i = start; i < nums.length; i++) {5 if (nums[i]8 > max2) {6 max = nums[i]8;7 }console.log("start=" + start);
7 }8}910console.log("start=" + start2);11console.log("max=" + max8);outputstart=2 max=8
Follow the Scan
numsstarts as[3, 7, 2, 8, 5].- The default
startis0, somaxbegins at3. - The loop checks each value from index
0onward. maxchanges to7, then to8.- The script prints
start=0andmax=8. | index | value | max after check | | --- | --- | --- | | 0 | 3 | 3 | | 1 | 7 | 7 | | 2 | 2 | 7 | | 3 | 8 | 8 | | 4 | 5 | 8 |
Exercise: indexed_loop.js
Reproduce start=0 and max=8, then use the pinned start variants 1 and 2 to predict that max stays 8 while the start line changes.