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