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