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 = [1, 12, 16, 10];
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 steps = [3, 13, 13];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
3.🔁 What number does this code show?
let scores = [29, 45, 54, 34];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 50) {
count = count + 1;
}
}
show(count); -
4.🔁 What number does this code show?
let steps = [86, 15, 93, 61];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 50) {
count = count + 1;
}
}
show(count); -
5.🔁 What number does this code show?
let scores = [20, 8, 7, 16];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
6.🔁 What number does this code show?
let steps = [19, 38, 72, 70, 25];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 60) {
count = count + 1;
}
}
show(count); -
7.🔁 What number does this code show?
let scores = [5, 17, 17, 10, 18];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
8.🔁 What number does this code show?
let marks = [1, 17, 12];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
9.🔁 What number does this code show?
let steps = [8, 12, 9];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
10.🔁 What is the LAST line this code shows?
let names = ["Riya", "Zoya", "Kabir", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Kabir (c) Hi, Aman (d) Hi, Zoya -
11.🔁 What number does this code show?
let marks = [1, 11, 6, 9, 13];
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 = [28, 59, 18];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 40) {
count = count + 1;
}
}
show(count); -
13.🔁 What is the LAST line this code shows?
let names = ["Aman", "Kabir", "Meera", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Zoya (c) Hi, Aman (d) Hi, Kabir -
14.🔁 What number does this code show?
let runs = [62, 26, 75, 36, 12];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 70) {
count = count + 1;
}
}
show(count); -
15.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Aman", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Riya (c) Hi, Kabir -
16.🔁 What number does this code show?
let scores = [2, 6, 3, 13, 3];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
17.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Meera", "Riya", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Aman (c) Hi, Meera (d) Hi, Arjun -
18.🔁 What is the LAST line this code shows?
let names = ["Meera", "Zoya", "Aman", "Riya", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Zoya (b) Hi, Aman (c) Hi, Meera (d) Hi, Arjun -
19.🔁 What number does this code show?
let steps = [30, 10, 83, 72];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 60) {
count = count + 1;
}
}
show(count); -
20.🔁 What is the LAST line this code shows?
let names = ["Aman", "Riya", "Zoya", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Aman (c) Hi, Meera (d) Hi, Zoya -
21.🔁 What number does this code show?
let runs = [93, 11, 99, 81, 49];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 50) {
count = count + 1;
}
}
show(count); - 22.🔁 list.length tells you ___. (a) how many items it has (b) the last item (c) the first item (d) the biggest item
-
23.🔁 What number does this code show?
let scores = [48, 24, 41];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 70) {
count = count + 1;
}
}
show(count); -
24.🔁 What is the LAST line this code shows?
let names = ["Arjun", "Aman", "Riya", "Kabir", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Arjun (b) Hi, Riya (c) Hi, Aman (d) Hi, Zoya -
25.🔁 What number does this code show?
let runs = [85, 96, 42, 13];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 70) {
count = count + 1;
}
}
show(count); -
26.🔁 What number does this code show?
let runs = [10, 19, 10];
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 marks = [10, 17, 14];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
28.🔁 What number does this code show?
let scores = [6, 1, 3, 7, 10];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); -
29.🔁 What number does this code show?
let steps = [98, 78, 15, 88, 92];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 50) {
count = count + 1;
}
}
show(count); -
30.🔁 What number does this code show?
let runs = [13, 12, 20, 2, 20];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
31.🔁 What number does this code show?
let marks = [6, 6, 2, 3, 4];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
32.🔁 What number does this code show?
let scores = [2, 18, 6, 14, 4];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
33.🔁 What is the LAST line this code shows?
let names = ["Aman", "Meera", "Arjun", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Arjun (c) Hi, Zoya (d) Hi, Aman -
34.🔁 What number does this code show?
let scores = [10, 16, 5];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
35.🔁 What number does this code show?
let runs = [7, 16, 18];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); -
36.🔁 What number does this code show?
let scores = [2, 9, 11, 1];
let best = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > best) {
best = scores[i];
}
}
show(best); - 37.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 3 (b) 0, 1, 2 (c) 0, 1, 2, 3 (d) 1, 2, 3
-
38.🔁 What number does this code show?
let marks = [4, 17, 4, 2];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
39.🔁 What number does this code show?
let marks = [12, 3, 3, 9, 13];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
40.🔁 What number does this code show?
let steps = [10, 2, 5, 11, 12];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total);
Answer key
- 39
- 29
- 1
- 3
- 51
- 2
- 67
- 30
- 29
- Hi, Aman
- 40
- 1
- Hi, Zoya
- 1
- Hi, Riya
- 27
- Hi, Aman
- Hi, Arjun
- 2
- Hi, Meera
- 3
- how many items it has
- 0
- Hi, Zoya
- 2
- 39
- 41
- 10
- 4
- 67
- 21
- 44
- Hi, Zoya
- 31
- 18
- 11
- 0, 1, 2
- 27
- 40
- 40
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students