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