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