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