Arrays and Iteration
For-Of Iteration
Visit each element of an array with for-of.
for-of
A `for...of` loop reads each element in turn without needing an index. Here it accumulates a running total.
For-Of Iteration
for_of.js
Replay: real traced execution (multi-file project)
const factor = 2;
const nums = [1, 2, 3];
let total = 0;
for (const n of nums) {
total += n * factor;
}
console.log("factor=" + factor);
console.log("total=" + total);
const factor = 3;
const nums = [1, 2, 3];
let total = 0;
for (const n of nums) {
total += n * factor;
}
console.log("factor=" + factor);
console.log("total=" + total);
const factor = 5;
const nums = [1, 2, 3];
let total = 0;
for (const n of nums) {
total += n * factor;
}
console.log("factor=" + factor);
console.log("total=" + total);
factor ← 2
1const factor→ 2 = 2; //@factor=3, 52const nums = [1, 2, 3];nums ← 1,2,3
1const factor = 2; //@factor=3, 52const nums→ 1,2,3 = [1, 2, 3];3let total = 0;total ← 0
1const factor = 2; //@factor=3, 52const nums = [1, 2, 3];3let total→ 0 = 0;4for (const n of nums) {total ← 2, n ← 1
3let total = 0;4for (const n of nums) {5 total += n→ 1 * factor2;6}values this step2totaltotal ← 6, n ← 2
3let total = 0;4for (const n of nums) {5 total += n→ 2 * factor2;6}values this step6totaltotal ← 12, n ← 3
3let total = 0;4for (const n of nums) {5 total += n→ 3 * factor2;6}values this step12totalconsole.log("factor=" + factor);
5 total += n * factor;6}78console.log("factor=" + factor2);9console.log("total=" + total);outputfactor=2console.log("total=" + total);
8console.log("factor=" + factor);9console.log("total=" + total12);outputtotal=12
factor ← 3
1const factor→ 3 = 3;2const nums = [1, 2, 3];nums ← 1,2,3
1const factor = 3;2const nums→ 1,2,3 = [1, 2, 3];3let total = 0;total ← 0
1const factor = 3;2const nums = [1, 2, 3];3let total→ 0 = 0;4for (const n of nums) {total ← 3, n ← 1
3let total = 0;4for (const n of nums) {5 total += n→ 1 * factor3;6}values this step3totaltotal ← 9, n ← 2
3let total = 0;4for (const n of nums) {5 total += n→ 2 * factor3;6}values this step9totaltotal ← 18, n ← 3
3let total = 0;4for (const n of nums) {5 total += n→ 3 * factor3;6}values this step18totalconsole.log("factor=" + factor);
5 total += n * factor;6}78console.log("factor=" + factor3);9console.log("total=" + total);outputfactor=3console.log("total=" + total);
8console.log("factor=" + factor);9console.log("total=" + total18);outputtotal=18
factor ← 5
1const factor→ 5 = 5;2const nums = [1, 2, 3];nums ← 1,2,3
1const factor = 5;2const nums→ 1,2,3 = [1, 2, 3];3let total = 0;total ← 0
1const factor = 5;2const nums = [1, 2, 3];3let total→ 0 = 0;4for (const n of nums) {total ← 5, n ← 1
3let total = 0;4for (const n of nums) {5 total += n→ 1 * factor5;6}values this step5totaltotal ← 15, n ← 2
3let total = 0;4for (const n of nums) {5 total += n→ 2 * factor5;6}values this step15totaltotal ← 30, n ← 3
3let total = 0;4for (const n of nums) {5 total += n→ 3 * factor5;6}values this step30totalconsole.log("factor=" + factor);
5 total += n * factor;6}78console.log("factor=" + factor5);9console.log("total=" + total);outputfactor=5console.log("total=" + total);
8console.log("factor=" + factor);9console.log("total=" + total30);outputtotal=30
Follow the Loop
factorstarts as2.numsis[1, 2, 3].- The loop visits each number once.
- Each pass adds
number * factortototal. - The script prints
factor=2andtotal=12. | number | added | running total | | --- | --- | --- | | 1 | 2 | 2 | | 2 | 4 | 6 | | 3 | 6 | 12 |
Exercise: for_of.js
Reproduce factor=2 and total=12, then use the pinned factor variants 3 and 5 to predict totals 18 and 30.