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