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