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