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