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