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.🔁 What number does this code show?
let steps = [8, 17, 7, 9];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
2.🔁 What number does this code show?
let scores = [10, 2, 16, 14];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); - 3.🔁 A loop over a list of 5 items runs its body how many times? (a) 4 (b) 1 (c) 5 (d) 6
-
4.🔁 What number does this code show?
let scores = [15, 8, 3, 5];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
5.🔁 What is the LAST line this code shows?
let names = ["Riya", "Zoya", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Meera (c) Hi, Zoya -
6.🔁 What number does this code show?
let scores = [18, 9, 15, 13, 19];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
7.🔁 What number does this code show?
let runs = [10, 1, 2, 2];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); - 8.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 1, 2, 3 (b) 3 (c) 0, 1, 2, 3 (d) 0, 1, 2
-
9.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Meera", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Meera (c) Hi, Arjun -
10.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Aman", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Arjun (b) Hi, Zoya (c) Hi, Aman -
11.🔁 What is the LAST line this code shows?
let names = ["Aman", "Zoya", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Zoya (b) Hi, Aman (c) Hi, Riya - 12.🔁 list.length tells you ___. (a) the last item (b) how many items it has (c) the biggest item (d) the first item
-
13.🔁 What number does this code show?
let runs = [19, 13, 11, 7];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
14.🔁 What is the LAST line this code shows?
let names = ["Meera", "Aman", "Riya", "Zoya", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Meera (c) Hi, Aman (d) Hi, Riya -
15.🔁 What number does this code show?
let steps = [8, 9, 5, 7];
let best = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] > best) {
best = steps[i];
}
}
show(best); -
16.🔁 What number does this code show?
let steps = [15, 2, 5];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
17.🔁 What number does this code show?
let scores = [14, 14, 3, 12, 15];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
18.🔁 What number does this code show?
let scores = [11, 7, 3];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
19.🔁 What number does this code show?
let scores = [55, 26, 34, 96];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 40) {
count = count + 1;
}
}
show(count); -
20.🔁 What number does this code show?
let runs = [6, 3, 9];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); - 21.🔁 Why use a loop to show 100 messages? (a) it uses more lines (b) it hides them (c) less code to write (d) it is slower
- 22.🔁 What does a loop do? (a) repeats code (b) deletes code (c) draws icons (d) saves photos
-
23.🔁 What number does this code show?
let runs = [6, 3, 7, 12, 20];
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 marks = [11, 5, 11, 15, 1];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
25.🔁 What number does this code show?
let steps = [87, 18, 30, 90];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 40) {
count = count + 1;
}
}
show(count); -
26.🔁 What number does this code show?
let scores = [11, 10, 10];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
27.🔁 What number does this code show?
let scores = [15, 10, 10];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
28.🔁 What number does this code show?
let steps = [16, 7, 17, 3];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
29.🔁 What number does this code show?
let scores = [28, 87, 70];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 70) {
count = count + 1;
}
}
show(count); -
30.🔁 What number does this code show?
let scores = [11, 13, 10, 13, 4];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
31.🔁 What number does this code show?
let marks = [16, 1, 20];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
32.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Riya", "Kabir", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Arjun (c) Hi, Aman (d) Hi, Kabir -
33.🔁 What number does this code show?
let runs = [43, 72, 95];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 50) {
count = count + 1;
}
}
show(count); -
34.🔁 What number does this code show?
let scores = [5, 10, 6, 12];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
35.🔁 What number does this code show?
let marks = [13, 19, 3];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
36.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Riya", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Riya (c) Hi, Aman -
37.🔁 What is the LAST line this code shows?
let names = ["Riya", "Meera", "Kabir", "Arjun", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Meera (c) Hi, Kabir (d) Hi, Aman -
38.🔁 What number does this code show?
let scores = [16, 16, 9];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
39.🔁 What number does this code show?
let runs = [44, 23, 20, 28];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 70) {
count = count + 1;
}
}
show(count); -
40.🔁 What number does this code show?
let steps = [13, 13, 9];
let best = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] > best) {
best = steps[i];
}
}
show(best);
Answer key
- 41
- 42
- 5
- 31
- Hi, Meera
- 19
- 10
- 0, 1, 2
- Hi, Kabir
- Hi, Zoya
- Hi, Riya
- how many items it has
- 50
- Hi, Kabir
- 9
- 22
- 58
- 21
- 2
- 9
- less code to write
- repeats code
- 20
- 43
- 2
- 31
- 15
- 43
- 2
- 13
- 37
- Hi, Aman
- 2
- 12
- 35
- Hi, Aman
- Hi, Aman
- 16
- 0
- 13
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students