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