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