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