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