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