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