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