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