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