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