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