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