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