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