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) 0, 1, 2, 3 (b) 3 (c) 0, 1, 2 (d) 1, 2, 3
-
2.🔁 What number does this code show?
let marks = [6, 20, 13];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
3.🔁 What number does this code show?
let scores = [5, 18, 15];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); - 4.🔁 What does a loop do? (a) repeats code (b) saves photos (c) draws icons (d) deletes code
-
5.🔁 What number does this code show?
let marks = [86, 38, 87, 33];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 60) {
count = count + 1;
}
}
show(count); -
6.🔁 What number does this code show?
let runs = [12, 33, 85, 28];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 50) {
count = count + 1;
}
}
show(count); -
7.🔁 What number does this code show?
let runs = [74, 40, 22, 52];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 60) {
count = count + 1;
}
}
show(count); -
8.🔁 What number does this code show?
let steps = [10, 9, 10, 17, 18];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
9.🔁 What is the LAST line this code shows?
let names = ["Aman", "Arjun", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Arjun (b) Hi, Aman (c) Hi, Riya -
10.🔁 What number does this code show?
let marks = [14, 5, 14, 3];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
11.🔁 What number does this code show?
let scores = [17, 5, 1, 19, 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 runs = [11, 18, 3, 2, 8];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
13.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Meera", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Meera (c) Hi, Arjun -
14.🔁 What number does this code show?
let runs = [59, 25, 79, 81];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 70) {
count = count + 1;
}
}
show(count); -
15.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Riya", "Meera", "Aman", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Zoya (c) Hi, Riya (d) Hi, Meera -
16.🔁 What number does this code show?
let scores = [3, 14, 16];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); - 17.🔁 A loop over a list of 5 items runs its body how many times? (a) 6 (b) 4 (c) 5 (d) 1
-
18.🔁 What number does this code show?
let scores = [17, 4, 8, 18];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
19.🔁 What is the LAST line this code shows?
let names = ["Riya", "Zoya", "Aman", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Zoya (c) Hi, Arjun (d) Hi, Aman -
20.🔁 What number does this code show?
let steps = [86, 87, 70];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 70) {
count = count + 1;
}
}
show(count); -
21.🔁 What number does this code show?
let marks = [9, 13, 5, 5];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
22.🔁 What number does this code show?
let runs = [3, 19, 20, 3];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
23.🔁 What is the LAST line this code shows?
let names = ["Aman", "Riya", "Zoya", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Riya (c) Hi, Zoya (d) Hi, Arjun -
24.🔁 What number does this code show?
let steps = [4, 12, 10];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); - 25.🔁 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
-
26.🔁 What is the LAST line this code shows?
let names = ["Meera", "Zoya", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Arjun (b) Hi, Zoya (c) Hi, Meera - 27.🔁 list.length tells you ___. (a) the biggest item (b) the last item (c) how many items it has (d) the first item
-
28.🔁 What is the LAST line this code shows?
let names = ["Zoya", "Arjun", "Kabir", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Arjun (c) Hi, Zoya (d) Hi, Kabir -
29.🔁 What number does this code show?
let scores = [79, 48, 90];
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 steps = [2, 13, 6];
let best = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] > best) {
best = steps[i];
}
}
show(best); -
31.🔁 What number does this code show?
let scores = [20, 8, 13, 8];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
32.🔁 What number does this code show?
let marks = [5, 3, 4, 2];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
33.🔁 What number does this code show?
let scores = [15, 34, 66, 32, 85];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 70) {
count = count + 1;
}
}
show(count); -
34.🔁 What number does this code show?
let steps = [43, 16, 20, 87];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 40) {
count = count + 1;
}
}
show(count); -
35.🔁 What number does this code show?
let scores = [15, 20, 9, 9, 8];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
36.🔁 What is the LAST line this code shows?
let names = ["Aman", "Kabir", "Riya", "Zoya", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Kabir (c) Hi, Riya (d) Hi, Meera -
37.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Riya", "Zoya", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Zoya (b) Hi, Riya (c) Hi, Kabir (d) Hi, Aman -
38.🔁 What number does this code show?
let scores = [61, 12, 19, 93, 85];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 40) {
count = count + 1;
}
}
show(count); -
39.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Aman", "Zoya", "Meera", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Zoya (c) Hi, Kabir (d) Hi, Arjun -
40.🔁 What number does this code show?
let runs = [32, 35, 53];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 40) {
count = count + 1;
}
}
show(count);
Answer key
- 0, 1, 2
- 39
- 38
- repeats code
- 2
- 1
- 1
- 64
- Hi, Riya
- 36
- 46
- 42
- Hi, Aman
- 2
- Hi, Zoya
- 33
- 5
- 47
- Hi, Arjun
- 3
- 32
- 45
- Hi, Arjun
- 26
- less code to write
- Hi, Arjun
- how many items it has
- Hi, Meera
- 2
- 13
- 49
- 5
- 1
- 2
- 61
- Hi, Meera
- Hi, Aman
- 3
- Hi, Arjun
- 1
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students