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