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