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