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