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