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