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