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