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