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