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