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