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