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