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