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