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