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) 1, 2, 3 (d) 0, 1, 2, 3
-
2.🔁 What number does this code show?
let scores = [11, 1, 7, 5];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); - 3.🔁 What does a loop do? (a) repeats code (b) deletes code (c) saves photos (d) draws icons
-
4.🔁 What is the LAST line this code shows?
let names = ["Zoya", "Arjun", "Meera", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Zoya (c) Hi, Arjun (d) Hi, Meera -
5.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Aman", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Arjun (c) Hi, Riya -
6.🔁 What number does this code show?
let marks = [5, 19, 1, 18, 1];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); - 7.🔁 A loop over a list of 5 items runs its body how many times? (a) 1 (b) 5 (c) 4 (d) 6
-
8.🔁 What number does this code show?
let scores = [77, 81, 51, 40, 26];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 40) {
count = count + 1;
}
}
show(count); -
9.🔁 What is the LAST line this code shows?
let names = ["Riya", "Aman", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Aman (c) Hi, Kabir -
10.🔁 What is the LAST line this code shows?
let names = ["Aman", "Meera", "Zoya", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Meera (c) Hi, Zoya (d) Hi, Arjun -
11.🔁 What number does this code show?
let marks = [53, 22, 94, 58, 52];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 60) {
count = count + 1;
}
}
show(count); -
12.🔁 What is the LAST line this code shows?
let names = ["Meera", "Riya", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Riya (c) Hi, Arjun -
13.🔁 What number does this code show?
let scores = [1, 7, 13, 4, 15];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
14.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Riya", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Riya (c) Hi, Kabir -
15.🔁 What number does this code show?
let marks = [3, 10, 4, 16];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
16.🔁 What is the LAST line this code shows?
let names = ["Riya", "Kabir", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Kabir (c) Hi, Riya -
17.🔁 What number does this code show?
let scores = [28, 33, 41, 16];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 50) {
count = count + 1;
}
}
show(count); -
18.🔁 What is the LAST line this code shows?
let names = ["Meera", "Zoya", "Kabir", "Arjun", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Kabir (c) Hi, Zoya (d) Hi, Meera -
19.🔁 What number does this code show?
let scores = [7, 13, 12, 5, 20];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
20.🔁 What number does this code show?
let steps = [4, 8, 16, 4];
let best = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] > best) {
best = steps[i];
}
}
show(best); -
21.🔁 What number does this code show?
let scores = [12, 3, 16, 20, 3];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
22.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Aman", "Kabir", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Riya (c) Hi, Arjun (d) Hi, Aman -
23.🔁 What number does this code show?
let scores = [36, 13, 94, 58];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 70) {
count = count + 1;
}
}
show(count); -
24.🔁 What number does this code show?
let marks = [7, 5, 19];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
25.🔁 What number does this code show?
let runs = [97, 10, 88, 16];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 50) {
count = count + 1;
}
}
show(count); -
26.🔁 What number does this code show?
let steps = [10, 48, 86, 47];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 40) {
count = count + 1;
}
}
show(count); -
27.🔁 What number does this code show?
let scores = [11, 8, 1];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); - 28.🔁 Why use a loop to show 100 messages? (a) it is slower (b) it hides them (c) less code to write (d) it uses more lines
-
29.🔁 What number does this code show?
let runs = [23, 42, 86, 19, 94];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 50) {
count = count + 1;
}
}
show(count); -
30.🔁 What number does this code show?
let runs = [24, 79, 24];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 40) {
count = count + 1;
}
}
show(count); -
31.🔁 What number does this code show?
let runs = [5, 9, 2, 11, 15];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
32.🔁 What number does this code show?
let scores = [91, 61, 53];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 40) {
count = count + 1;
}
}
show(count); -
33.🔁 What number does this code show?
let steps = [1, 17, 16, 4, 19];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
34.🔁 What number does this code show?
let scores = [14, 6, 14];
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 marks = [78, 36, 72, 85];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 50) {
count = count + 1;
}
}
show(count); -
36.🔁 What number does this code show?
let scores = [15, 1, 15, 16];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
37.🔁 What number does this code show?
let scores = [17, 18, 8, 2, 10];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
38.🔁 What number does this code show?
let scores = [5, 7, 6, 10];
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 marks = [81, 80, 37];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 40) {
count = count + 1;
}
}
show(count); -
40.🔁 What number does this code show?
let marks = [16, 5, 20];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total);
Answer key
- 0, 1, 2
- 24
- repeats code
- Hi, Riya
- Hi, Riya
- 19
- 5
- 4
- Hi, Kabir
- Hi, Arjun
- 1
- Hi, Arjun
- 40
- Hi, Meera
- 33
- Hi, Aman
- 0
- Hi, Riya
- 20
- 16
- 54
- Hi, Riya
- 1
- 19
- 2
- 3
- 20
- less code to write
- 2
- 1
- 42
- 3
- 57
- 34
- 3
- 16
- 18
- 10
- 2
- 41
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students