App Dev Worksheet: Loops over Lists
Developer's tip (One by one): for (let i = 0; i < list.length; i++) visits every item, from position 0. Total: add each item. Count: add 1 when the test is true. Best: keep the biggest so far. The body runs once per item: a list of 5 runs it 5 times.
NameDateTime takenScore ___ / 40
- 1.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 3 (b) 0, 1, 2 (c) 0, 1, 2, 3 (d) 1, 2, 3
-
2.🔁 What is the LAST line this code shows?
let names = ["Riya", "Meera", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Kabir (c) Hi, Riya -
3.🔁 What number does this code show?
let steps = [44, 75, 71];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 60) {
count = count + 1;
}
}
show(count); - 4.🔁 list.length tells you ___. (a) the last item (b) the biggest item (c) the first item (d) how many items it has
-
5.🔁 What number does this code show?
let scores = [3, 5, 10, 14];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
6.🔁 What number does this code show?
let scores = [19, 2, 6];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); - 7.🔁 Why use a loop to show 100 messages? (a) it uses more lines (b) less code to write (c) it is slower (d) it hides them
-
8.🔁 What number does this code show?
let runs = [98, 75, 70, 62];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 40) {
count = count + 1;
}
}
show(count); -
9.🔁 What number does this code show?
let steps = [18, 7, 15, 13, 11];
let best = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] > best) {
best = steps[i];
}
}
show(best); -
10.🔁 What number does this code show?
let scores = [20, 14, 5];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
11.🔁 What is the LAST line this code shows?
let names = ["Aman", "Meera", "Kabir", "Riya", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Arjun (b) Hi, Kabir (c) Hi, Meera (d) Hi, Aman -
12.🔁 What number does this code show?
let marks = [3, 14, 14];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
13.🔁 What is the LAST line this code shows?
let names = ["Zoya", "Aman", "Meera", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Zoya (c) Hi, Kabir (d) Hi, Meera -
14.🔁 What number does this code show?
let scores = [7, 14, 2, 20];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); - 15.🔁 What does a loop do? (a) draws icons (b) repeats code (c) deletes code (d) saves photos
-
16.🔁 What number does this code show?
let runs = [8, 12, 1, 19];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); -
17.🔁 What number does this code show?
let scores = [16, 15, 8, 2, 7];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); - 18.🔁 A loop over a list of 5 items runs its body how many times? (a) 4 (b) 6 (c) 5 (d) 1
-
19.🔁 What number does this code show?
let runs = [15, 1, 17, 5];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
20.🔁 What number does this code show?
let steps = [38, 32, 94, 30];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 50) {
count = count + 1;
}
}
show(count); -
21.🔁 What number does this code show?
let marks = [16, 3, 3];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
22.🔁 What number does this code show?
let scores = [12, 13, 4, 2, 20];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
23.🔁 What number does this code show?
let runs = [19, 4, 1, 11];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); -
24.🔁 What number does this code show?
let runs = [20, 7, 15, 10];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
25.🔁 What is the LAST line this code shows?
let names = ["Zoya", "Riya", "Aman", "Kabir", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Riya (c) Hi, Meera (d) Hi, Zoya -
26.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Arjun", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Arjun (b) Hi, Riya (c) Hi, Kabir -
27.🔁 What number does this code show?
let steps = [14, 66, 18, 86];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 40) {
count = count + 1;
}
}
show(count); -
28.🔁 What number does this code show?
let steps = [70, 56, 87, 39];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 70) {
count = count + 1;
}
}
show(count); -
29.🔁 What number does this code show?
let scores = [10, 3, 1];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
30.🔁 What number does this code show?
let steps = [8, 7, 10];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
31.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Riya", "Meera", "Kabir", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Meera (c) Hi, Aman (d) Hi, Arjun -
32.🔁 What number does this code show?
let runs = [5, 3, 11, 1, 6];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
33.🔁 What number does this code show?
let marks = [17, 21, 36, 13, 34];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 50) {
count = count + 1;
}
}
show(count); -
34.🔁 What number does this code show?
let scores = [16, 3, 15, 12, 15];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
35.🔁 What number does this code show?
let scores = [11, 1, 9];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
36.🔁 What is the LAST line this code shows?
let names = ["Meera", "Aman", "Riya", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Riya (c) Hi, Arjun (d) Hi, Aman -
37.🔁 What number does this code show?
let scores = [15, 15, 3];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
38.🔁 What number does this code show?
let marks = [18, 5, 1, 20, 16];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
39.🔁 What number does this code show?
let steps = [5, 14, 19];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
40.🔁 What number does this code show?
let steps = [1, 4, 4, 4];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total);
Answer key
- 0, 1, 2
- Hi, Kabir
- 2
- how many items it has
- 32
- 19
- less code to write
- 4
- 18
- 20
- Hi, Arjun
- 14
- Hi, Kabir
- 43
- repeats code
- 19
- 16
- 5
- 38
- 1
- 16
- 51
- 19
- 52
- Hi, Meera
- Hi, Riya
- 2
- 2
- 14
- 25
- Hi, Aman
- 26
- 0
- 61
- 11
- Hi, Arjun
- 33
- 20
- 38
- 13
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students