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