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