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