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 number does this code show?
let marks = [2, 2, 5, 7, 2];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
2.🔁 What number does this code show?
let runs = [94, 95, 11];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 70) {
count = count + 1;
}
}
show(count); - 3.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 1, 2, 3 (b) 3 (c) 0, 1, 2 (d) 0, 1, 2, 3
-
4.🔁 What number does this code show?
let steps = [26, 50, 19];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 60) {
count = count + 1;
}
}
show(count); - 5.🔁 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
- 6.🔁 A loop over a list of 5 items runs its body how many times? (a) 4 (b) 6 (c) 1 (d) 5
-
7.🔁 What number does this code show?
let marks = [14, 19, 12];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
8.🔁 What number does this code show?
let scores = [12, 1, 8];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
9.🔁 What is the LAST line this code shows?
let names = ["Riya", "Aman", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Aman (c) Hi, Zoya -
10.🔁 What number does this code show?
let steps = [93, 59, 53];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 70) {
count = count + 1;
}
}
show(count); -
11.🔁 What number does this code show?
let marks = [6, 16, 14];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
12.🔁 What number does this code show?
let runs = [16, 18, 10, 5, 2];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); -
13.🔁 What is the LAST line this code shows?
let names = ["Riya", "Zoya", "Aman", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Zoya (b) Hi, Riya (c) Hi, Aman (d) Hi, Arjun -
14.🔁 What number does this code show?
let marks = [20, 18, 14];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
15.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Riya", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Arjun (b) Hi, Aman (c) Hi, Riya -
16.🔁 What is the LAST line this code shows?
let names = ["Meera", "Aman", "Zoya", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Aman (c) Hi, Meera (d) Hi, Zoya -
17.🔁 What number does this code show?
let marks = [10, 7, 9];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
18.🔁 What number does this code show?
let runs = [19, 19, 19, 12, 4];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); - 19.🔁 list.length tells you ___. (a) the biggest item (b) the last item (c) how many items it has (d) the first item
-
20.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Aman", "Arjun", "Riya", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Meera (c) Hi, Arjun (d) Hi, Aman -
21.🔁 What number does this code show?
let scores = [61, 84, 99, 34, 30];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 70) {
count = count + 1;
}
}
show(count); -
22.🔁 What number does this code show?
let marks = [16, 10, 13];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
23.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Meera", "Kabir", "Zoya", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Arjun (c) Hi, Kabir (d) Hi, Meera -
24.🔁 What number does this code show?
let marks = [4, 2, 20];
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 scores = [2, 2, 1, 2, 12];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
26.🔁 What number does this code show?
let runs = [7, 1, 14];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
27.🔁 What number does this code show?
let scores = [20, 16, 13];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
28.🔁 What number does this code show?
let marks = [2, 3, 19, 12];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
29.🔁 What number does this code show?
let marks = [16, 3, 6, 9, 9];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
30.🔁 What is the LAST line this code shows?
let names = ["Aman", "Riya", "Arjun", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Aman (c) Hi, Arjun (d) Hi, Meera -
31.🔁 What is the LAST line this code shows?
let names = ["Aman", "Arjun", "Zoya", "Kabir", "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 -
32.🔁 What number does this code show?
let steps = [7, 20, 9];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
33.🔁 What number does this code show?
let scores = [91, 55, 31, 81, 57];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 70) {
count = count + 1;
}
}
show(count); -
34.🔁 What number does this code show?
let runs = [9, 17, 3, 14, 12];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); -
35.🔁 What number does this code show?
let runs = [17, 18, 6];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
36.🔁 What number does this code show?
let steps = [7, 5, 15];
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 = [55, 61, 49];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 50) {
count = count + 1;
}
}
show(count); -
38.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Kabir", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Riya (c) Hi, Arjun -
39.🔁 What number does this code show?
let scores = [11, 49, 55, 60, 55];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 50) {
count = count + 1;
}
}
show(count); -
40.🔁 What number does this code show?
let marks = [20, 1, 1, 18, 6];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best);
Answer key
- 7
- 2
- 0, 1, 2
- 0
- less code to write
- 5
- 45
- 12
- Hi, Zoya
- 1
- 36
- 18
- Hi, Arjun
- 20
- Hi, Aman
- Hi, Riya
- 10
- 19
- how many items it has
- Hi, Meera
- 2
- 39
- Hi, Riya
- 26
- 12
- 22
- 49
- 19
- 43
- Hi, Meera
- Hi, Riya
- 36
- 2
- 17
- 41
- 15
- 2
- Hi, Riya
- 3
- 20
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students