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