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