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