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