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