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