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