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