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