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