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