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