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