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