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