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 scores = [5, 5, 10];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
2.🔁 What number does this code show?
let scores = [2, 4, 19];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
3.🔁 What number does this code show?
let scores = [4, 16, 1, 2, 15];
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 scores = [39, 74, 74, 35, 50];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 70) {
count = count + 1;
}
}
show(count); -
5.🔁 What number does this code show?
let marks = [7, 6, 13, 3, 6];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
6.🔁 What number does this code show?
let scores = [16, 3, 12];
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 steps = [27, 16, 90];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 50) {
count = count + 1;
}
}
show(count); -
8.🔁 What number does this code show?
let runs = [14, 18, 12, 11, 13];
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 scores = [1, 12, 2];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
10.🔁 What number does this code show?
let steps = [12, 11, 20];
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 = [11, 18, 18];
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 marks = [7, 11, 3, 19, 6];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); - 13.🔁 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
-
14.🔁 What is the LAST line this code shows?
let names = ["Zoya", "Riya", "Aman", "Arjun", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Riya (c) Hi, Zoya (d) Hi, Kabir -
15.🔁 What number does this code show?
let runs = [21, 95, 62];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 40) {
count = count + 1;
}
}
show(count); -
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, Aman (b) Hi, Riya (c) Hi, Kabir -
17.🔁 What is the LAST line this code shows?
let names = ["Riya", "Arjun", "Kabir", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Arjun (c) Hi, Aman (d) Hi, Riya -
18.🔁 What number does this code show?
let scores = [7, 15, 11, 10, 14];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
19.🔁 What number does this code show?
let runs = [5, 1, 2];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); -
20.🔁 What is the LAST line this code shows?
let names = ["Meera", "Zoya", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Zoya (b) Hi, Riya (c) Hi, Meera -
21.🔁 What number does this code show?
let steps = [16, 14, 20, 6, 10];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
22.🔁 What number does this code show?
let scores = [18, 19, 1];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
23.🔁 What number does this code show?
let scores = [10, 9, 5, 10];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
24.🔁 What number does this code show?
let marks = [17, 46, 74];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 70) {
count = count + 1;
}
}
show(count); -
25.🔁 What number does this code show?
let scores = [12, 15, 6];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
26.🔁 What number does this code show?
let marks = [3, 8, 15, 18];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
27.🔁 What is the LAST line this code shows?
let names = ["Zoya", "Riya", "Aman", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Kabir (c) Hi, Riya (d) Hi, Zoya -
28.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Arjun", "Zoya", "Meera", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Zoya (b) Hi, Riya (c) Hi, Arjun (d) Hi, Kabir -
29.🔁 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, Aman (b) Hi, Kabir (c) Hi, Meera -
30.🔁 What number does this code show?
let marks = [3, 13, 17, 18, 8];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
31.🔁 What number does this code show?
let marks = [6, 17, 19, 7];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
32.🔁 What number does this code show?
let scores = [24, 32, 86];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 50) {
count = count + 1;
}
}
show(count); -
33.🔁 What is the LAST line this code shows?
let names = ["Aman", "Kabir", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Aman (c) Hi, Zoya -
34.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Zoya", "Arjun", "Riya", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Aman (c) Hi, Zoya (d) Hi, Arjun -
35.🔁 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 -
36.🔁 What number does this code show?
let marks = [2, 17, 12, 7];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
37.🔁 What is the LAST line this code shows?
let names = ["Zoya", "Aman", "Arjun", "Meera", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Riya (c) Hi, Arjun (d) Hi, Zoya - 38.🔁 What does a loop do? (a) saves photos (b) deletes code (c) repeats code (d) draws icons
-
39.🔁 What number does this code show?
let scores = [61, 62, 36, 16];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 60) {
count = count + 1;
}
}
show(count); - 40.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 0, 1, 2 (b) 0, 1, 2, 3 (c) 1, 2, 3 (d) 3
Answer key
- 20
- 19
- 38
- 2
- 35
- 16
- 1
- 18
- 15
- 43
- 18
- 19
- less code to write
- Hi, Kabir
- 2
- Hi, Aman
- Hi, Aman
- 57
- 5
- Hi, Riya
- 66
- 19
- 10
- 1
- 33
- 18
- Hi, Kabir
- Hi, Riya
- Hi, Kabir
- 18
- 49
- 1
- Hi, Zoya
- Hi, Aman
- Hi, Aman
- 17
- Hi, Riya
- repeats code
- 2
- 0, 1, 2
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students