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