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