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