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