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