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