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