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