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