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