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