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