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