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