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