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