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