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