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