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