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