Walk an array once, accumulating each element into a running total. This is the canonical single-pass linear scan and the simplest possible loop invariant: after step i, total equals the sum of arr[0..i].

Algorithm

The canonical input from the lesson spec is arr = [3, 1, 4, 1, 5, 9, 2, 6]. After eight passes the running total is 31.

linear scan Visit each element exactly once in index order.
running total `total` accumulates the sum as the loop advances.

Basic Implementation

basic.ts
Replay: real traced execution (multi-file project)
const arr: number[] = [3, 1, 4, 1, 5, 9, 2, 6];
let total: number = 0;
for (let i: number = 0; i < arr.length; i++) {
    total = total + arr[i];
}
console.log(total);
  1. arr ← [3, 1, 4, 1, 5, 9, 2, 6]

    1const arr: number[] = [3, 1, 4, 1, 5, 9, 2, 6];2let total: number = 0;
    values this step[3, 1, 4, 1, 5, 9, 2, 6]arr
  2. total ← 0

    1const arr: number[] = [3, 1, 4, 1, 5, 9, 2, 6];2let total: number = 0;3for (let i: number = 0; i < arr.length; i++) {
    values this step0total[3, 1, 4, 1, 5, 9, 2, 6]arr
  3. total ← 3

    3for (let i: number = 0; i < arr.length; i++) {4    total = total + arr[i];5}
    values this step0 3total0i3arr[i]
  4. total ← 4

    3for (let i: number = 0; i < arr.length; i++) {4    total = total + arr[i];5}
    values this step3 4total1i1arr[i]
  5. total ← 8

    3for (let i: number = 0; i < arr.length; i++) {4    total = total + arr[i];5}
    values this step4 8total2i4arr[i]
  6. total ← 9

    3for (let i: number = 0; i < arr.length; i++) {4    total = total + arr[i];5}
    values this step8 9total3i1arr[i]
  7. total ← 14

    3for (let i: number = 0; i < arr.length; i++) {4    total = total + arr[i];5}
    values this step9 14total4i5arr[i]
  8. total ← 23

    3for (let i: number = 0; i < arr.length; i++) {4    total = total + arr[i];5}
    values this step14 23total5i9arr[i]
  9. total ← 25

    3for (let i: number = 0; i < arr.length; i++) {4    total = total + arr[i];5}
    values this step23 25total6i2arr[i]
  10. total ← 31

    3for (let i: number = 0; i < arr.length; i++) {4    total = total + arr[i];5}
    values this step25 31total7i6arr[i]

Trace Output

trace.ts
Replay: real traced execution (multi-file project)
const arr: number[] = [3, 1, 4, 1, 5, 9, 2, 6];
let total: number = 0;
for (let i: number = 0; i < arr.length; i++) {
    const before: number = total;
    total = total + arr[i];
    console.log(`step ${i}: arr[${i}]=${arr[i]} total ${before} -> ${total}`);
}
console.log(`final total = ${total}`);
  1. total ← 3, stdout ← step 0: arr[0]=3 total 0 -> 3

    4const before: number = total;5total = total + arr[i];6console.log(`step ${i}: arr[${i}]=${arr[i]} total ${before} -> ${total}`);
    values this step3totalstep 0: arr[0]=3 total 0 -> 3stdout0before3arr[i]
  2. total ← 4, stdout ← step 1: arr[1]=1 total 3 -> 4

    4const before: number = total;5total = total + arr[i];6console.log(`step ${i}: arr[${i}]=${arr[i]} total ${before} -> ${total}`);
    values this step4totalstep 1: arr[1]=1 total 3 -> 4stdout3before1arr[i]
  3. total ← 8, stdout ← step 2: arr[2]=4 total 4 -> 8

    4const before: number = total;5total = total + arr[i];6console.log(`step ${i}: arr[${i}]=${arr[i]} total ${before} -> ${total}`);
    values this step8totalstep 2: arr[2]=4 total 4 -> 8stdout4before4arr[i]
  4. total ← 9, stdout ← step 3: arr[3]=1 total 8 -> 9

    4const before: number = total;5total = total + arr[i];6console.log(`step ${i}: arr[${i}]=${arr[i]} total ${before} -> ${total}`);
    values this step9totalstep 3: arr[3]=1 total 8 -> 9stdout8before1arr[i]
  5. total ← 14, stdout ← step 4: arr[4]=5 total 9 -> 14

    4const before: number = total;5total = total + arr[i];6console.log(`step ${i}: arr[${i}]=${arr[i]} total ${before} -> ${total}`);
    values this step14totalstep 4: arr[4]=5 total 9 -> 14stdout9before5arr[i]
  6. total ← 23, stdout ← step 5: arr[5]=9 total 14 -> 23

    4const before: number = total;5total = total + arr[i];6console.log(`step ${i}: arr[${i}]=${arr[i]} total ${before} -> ${total}`);
    values this step23totalstep 5: arr[5]=9 total 14 -> 23stdout14before9arr[i]
  7. total ← 25, stdout ← step 6: arr[6]=2 total 23 -> 25

    4const before: number = total;5total = total + arr[i];6console.log(`step ${i}: arr[${i}]=${arr[i]} total ${before} -> ${total}`);
    values this step25totalstep 6: arr[6]=2 total 23 -> 25stdout23before2arr[i]
  8. total ← 31, stdout ← step 7: arr[7]=6 total 25 -> 31

    4const before: number = total;5total = total + arr[i];6console.log(`step ${i}: arr[${i}]=${arr[i]} total ${before} -> ${total}`);
    values this step31totalstep 7: arr[7]=6 total 25 -> 31stdout25before6arr[i]
  9. stdout ← final total = 31

    7}8console.log(`final total = ${total}`);
    values this stepfinal total = 31stdout31total

Complexity

  • Time: O(n)
  • Space: O(1)

Implementation notes

  • TypeScript: use the explicit for (let i: number = 0; i < arr.length; i++) loop and let total: number. Calling arr.reduce((a, b) => a + b, 0) would hide the iteration the lesson is teaching.
  • Type annotations (: number[], : number) keep the lesson honest about the integer-array contract without changing runtime behaviour.
  • The replay shows i, arr[i], and total before and after each addition, matching the lesson spec's state-transition table.