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

start
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);
  1. 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++) {
  2. for (let i = start; i < nums.length; i++)

    pass 1 of 5
    2const 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
    passinums[i]max
    10
    2173
    32
    4387
    54
  3. if (nums[i] > max)

    pass 1 of 2
    3let max = nums[start];4for (let i = start; i < nums.length; i++) {5  if (nums[i]7 > max3) {6    max = nums[i]7;7  }
  4. if (nums[i] > max)

    pass 2 of 2
    3let max = nums[start];4for (let i = start; i < nums.length; i++) {5  if (nums[i]8 > max7) {6    max = nums[i]8;7  }
  5. console.log("start=" + start);

    7  }8}910console.log("start=" + start0);11console.log("max=" + max8);
    outputstart=0
    max=8
  1. 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++) {
  2. for (let i = start; i < nums.length; i++)

    pass 1 of 4
    2const 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
    passinums[i]max
    11
    22
    3387
    44
  3. 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  }
  4. console.log("start=" + start);

    7  }8}910console.log("start=" + start1);11console.log("max=" + max8);
    outputstart=1
    max=8
  1. 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++) {
  2. for (let i = start; i < nums.length; i++)

    pass 1 of 3
    2const 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
    passinums[i]max
    12
    2382
    34
  3. 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  }
  4. console.log("start=" + start);

    7  }8}910console.log("start=" + start2);11console.log("max=" + max8);
    outputstart=2
    max=8

Follow the Scan

  1. nums starts as [3, 7, 2, 8, 5].
  2. The default start is 0, so max begins at 3.
  3. The loop checks each value from index 0 onward.
  4. max changes to 7, then to 8.
  5. The script prints start=0 and max=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.