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