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