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