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