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