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) 1, 2, 3 (b) 3 (c) 0, 1, 2, 3 (d) 0, 1, 2
-
2.🔁 What number does this code show?
let marks = [71, 70, 35, 98, 78];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 70) {
count = count + 1;
}
}
show(count); -
3.🔁 What number does this code show?
let marks = [11, 14, 20];
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 = ["Zoya", "Meera", "Kabir", "Arjun", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Aman (c) Hi, Kabir (d) Hi, Zoya -
5.🔁 What number does this code show?
let marks = [79, 14, 66];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 60) {
count = count + 1;
}
}
show(count); -
6.🔁 What number does this code show?
let scores = [5, 1, 6, 12, 2];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
7.🔁 What number does this code show?
let runs = [18, 1, 13, 11, 3];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
8.🔁 What number does this code show?
let scores = [14, 14, 8, 9, 17];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
9.🔁 What number does this code show?
let marks = [12, 60, 30, 11, 51];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 70) {
count = count + 1;
}
}
show(count); -
10.🔁 What is the LAST line this code shows?
let names = ["Meera", "Arjun", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Aman (c) Hi, Arjun -
11.🔁 What is the LAST line this code shows?
let names = ["Aman", "Kabir", "Riya", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Zoya (c) Hi, Kabir (d) Hi, Riya -
12.🔁 What number does this code show?
let runs = [5, 8, 10];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); -
13.🔁 What is the LAST line this code shows?
let names = ["Aman", "Meera", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Meera (c) Hi, Riya - 14.🔁 list.length tells you ___. (a) the last item (b) the first item (c) how many items it has (d) the biggest item
-
15.🔁 What is the LAST line this code shows?
let names = ["Meera", "Arjun", "Zoya", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Arjun (b) Hi, Meera (c) Hi, Kabir (d) Hi, Zoya -
16.🔁 What is the LAST line this code shows?
let names = ["Riya", "Aman", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Arjun (c) Hi, Riya - 17.🔁 A loop over a list of 5 items runs its body how many times? (a) 4 (b) 6 (c) 5 (d) 1
-
18.🔁 What number does this code show?
let scores = [13, 19, 9, 17];
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 scores = [4, 20, 10];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
20.🔁 What is the LAST line this code shows?
let names = ["Riya", "Arjun", "Zoya", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Zoya (c) Hi, Arjun (d) Hi, Riya -
21.🔁 What number does this code show?
let marks = [5, 5, 10, 17];
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", "Zoya", "Riya", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Riya (c) Hi, Arjun (d) Hi, Zoya -
23.🔁 What is the LAST line this code shows?
let names = ["Meera", "Aman", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Aman (c) Hi, Meera - 24.🔁 What does a loop do? (a) repeats code (b) saves photos (c) deletes code (d) draws icons
-
25.🔁 What number does this code show?
let steps = [17, 19, 5];
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 = ["Meera", "Zoya", "Arjun", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Zoya (b) Hi, Riya (c) Hi, Arjun (d) Hi, Meera -
27.🔁 What number does this code show?
let scores = [11, 1, 8, 1, 16];
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 marks = [95, 38, 77, 49, 87];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 40) {
count = count + 1;
}
}
show(count); -
29.🔁 What number does this code show?
let steps = [41, 51, 45, 51];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 60) {
count = count + 1;
}
}
show(count); -
30.🔁 What number does this code show?
let steps = [38, 23, 39, 39, 46];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 50) {
count = count + 1;
}
}
show(count); -
31.🔁 What is the LAST line this code shows?
let names = ["Meera", "Aman", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Zoya (c) Hi, Aman -
32.🔁 What number does this code show?
let steps = [4, 17, 20, 1];
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 marks = [77, 33, 11, 16, 32];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 60) {
count = count + 1;
}
}
show(count); -
34.🔁 What is the LAST line this code shows?
let names = ["Aman", "Kabir", "Meera", "Arjun", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Zoya (b) Hi, Aman (c) Hi, Meera (d) Hi, Kabir -
35.🔁 What number does this code show?
let scores = [75, 26, 10, 86];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 70) {
count = count + 1;
}
}
show(count); - 36.🔁 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
-
37.🔁 What number does this code show?
let runs = [1, 8, 5];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
38.🔁 What is the LAST line this code shows?
let names = ["Aman", "Meera", "Arjun", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Aman (c) Hi, Meera (d) Hi, Arjun -
39.🔁 What number does this code show?
let scores = [18, 13, 12];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
40.🔁 What number does this code show?
let marks = [11, 16, 11];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best);
Answer key
- 0, 1, 2
- 4
- 45
- Hi, Aman
- 2
- 26
- 46
- 17
- 0
- Hi, Aman
- Hi, Zoya
- 10
- Hi, Riya
- how many items it has
- Hi, Kabir
- Hi, Arjun
- 5
- 58
- 20
- Hi, Aman
- 17
- Hi, Kabir
- Hi, Kabir
- repeats code
- 19
- Hi, Riya
- 16
- 4
- 0
- 0
- Hi, Zoya
- 42
- 1
- Hi, Zoya
- 2
- less code to write
- 14
- Hi, Riya
- 18
- 16
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students