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