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