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