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