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