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 does a loop do? (a) repeats code (b) draws icons (c) saves photos (d) deletes code
-
2.🔁 What number does this code show?
let runs = [86, 11, 29, 91];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 40) {
count = count + 1;
}
}
show(count); -
3.🔁 What is the LAST line this code shows?
let names = ["Meera", "Riya", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Riya (c) Hi, Meera -
4.🔁 What number does this code show?
let marks = [6, 16, 18, 7];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
5.🔁 What is the LAST line this code shows?
let names = ["Kabir", "Meera", "Arjun", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Arjun (b) Hi, Riya (c) Hi, Kabir (d) Hi, Meera -
6.🔁 What number does this code show?
let scores = [83, 69, 63, 72, 41];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 70) {
count = count + 1;
}
}
show(count); -
7.🔁 What number does this code show?
let runs = [16, 2, 16, 5, 4];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); -
8.🔁 What number does this code show?
let marks = [93, 32, 11];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 50) {
count = count + 1;
}
}
show(count); - 9.🔁 list.length tells you ___. (a) the last item (b) the first item (c) the biggest item (d) how many items it has
-
10.🔁 What number does this code show?
let scores = [1, 20, 4, 14, 8];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
11.🔁 What number does this code show?
let steps = [4, 3, 17, 9];
let best = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] > best) {
best = steps[i];
}
}
show(best); -
12.🔁 What number does this code show?
let marks = [36, 63, 83];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 50) {
count = count + 1;
}
}
show(count); -
13.🔁 What number does this code show?
let scores = [1, 2, 20];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
14.🔁 What number does this code show?
let steps = [16, 15, 8];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
15.🔁 What number does this code show?
let marks = [14, 3, 12, 14, 13];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
16.🔁 What is the LAST line this code shows?
let names = ["Aman", "Arjun", "Riya", "Zoya", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Meera (c) Hi, Arjun (d) Hi, Riya -
17.🔁 What is the LAST line this code shows?
let names = ["Meera", "Zoya", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Meera (b) Hi, Aman (c) Hi, Zoya -
18.🔁 What number does this code show?
let marks = [16, 16, 6, 5];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
19.🔁 What number does this code show?
let marks = [68, 94, 66, 72];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 40) {
count = count + 1;
}
}
show(count); -
20.🔁 What is the LAST line this code shows?
let names = ["Zoya", "Aman", "Riya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Zoya (b) Hi, Aman (c) Hi, Riya -
21.🔁 What is the LAST line this code shows?
let names = ["Meera", "Arjun", "Aman"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Arjun (c) Hi, Meera -
22.🔁 What is the LAST line this code shows?
let names = ["Meera", "Riya", "Zoya", "Arjun"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Meera (c) Hi, Zoya (d) Hi, Arjun -
23.🔁 What is the LAST line this code shows?
let names = ["Meera", "Riya", "Arjun", "Zoya"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Arjun (b) Hi, Meera (c) Hi, Zoya (d) Hi, Riya -
24.🔁 What number does this code show?
let steps = [5, 11, 1, 6];
let total = 0;
for (let i = 0; i < steps.length; i++) {
total = total + steps[i];
}
show(total); -
25.🔁 What number does this code show?
let steps = [62, 45, 15, 43];
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 runs = [1, 5, 15, 4, 17];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
27.🔁 What is the LAST line this code shows?
let names = ["Riya", "Zoya", "Aman", "Arjun", "Kabir"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Riya (b) Hi, Kabir (c) Hi, Aman (d) Hi, Zoya -
28.🔁 What number does this code show?
let steps = [13, 37, 84, 92, 36];
let count = 0;
for (let i = 0; i < steps.length; i++) {
if (steps[i] >= 60) {
count = count + 1;
}
}
show(count); -
29.🔁 What number does this code show?
let runs = [16, 20, 3, 7, 5];
let total = 0;
for (let i = 0; i < runs.length; i++) {
total = total + runs[i];
}
show(total); -
30.🔁 What number does this code show?
let marks = [4, 12, 7, 6];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
31.🔁 What is the LAST line this code shows?
let names = ["Aman", "Kabir", "Arjun", "Zoya", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Kabir (b) Hi, Arjun (c) Hi, Aman (d) Hi, Meera -
32.🔁 What number does this code show?
let marks = [19, 11, 1, 6, 6];
let total = 0;
for (let i = 0; i < marks.length; i++) {
total = total + marks[i];
}
show(total); -
33.🔁 What number does this code show?
let marks = [11, 49, 76];
let count = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] >= 60) {
count = count + 1;
}
}
show(count); -
34.🔁 What number does this code show?
let scores = [70, 51, 98];
let count = 0;
for (let i = 0; i < scores.length; i++) {
if (scores[i] >= 40) {
count = count + 1;
}
}
show(count); -
35.🔁 What number does this code show?
let scores = [11, 2, 5, 17];
let total = 0;
for (let i = 0; i < scores.length; i++) {
total = total + scores[i];
}
show(total); -
36.🔁 What is the LAST line this code shows?
let names = ["Aman", "Arjun", "Kabir", "Zoya", "Meera"];
for (let i = 0; i < names.length; i++) {
show("Hi, " + names[i]);
} (a) Hi, Aman (b) Hi, Meera (c) Hi, Kabir (d) Hi, Arjun -
37.🔁 What number does this code show?
let marks = [19, 15, 13, 1, 12];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best); -
38.🔁 What number does this code show?
let runs = [91, 20, 27, 20];
let count = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] >= 50) {
count = count + 1;
}
}
show(count); -
39.🔁 What number does this code show?
let runs = [6, 11, 12, 3];
let best = 0;
for (let i = 0; i < runs.length; i++) {
if (runs[i] > best) {
best = runs[i];
}
}
show(best); -
40.🔁 What number does this code show?
let marks = [5, 19, 8, 11];
let best = 0;
for (let i = 0; i < marks.length; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
show(best);
Answer key
- repeats code
- 2
- Hi, Aman
- 47
- Hi, Riya
- 2
- 16
- 1
- how many items it has
- 47
- 17
- 2
- 23
- 39
- 56
- Hi, Meera
- Hi, Aman
- 16
- 4
- Hi, Riya
- Hi, Aman
- Hi, Arjun
- Hi, Zoya
- 23
- 1
- 42
- Hi, Kabir
- 2
- 51
- 12
- Hi, Meera
- 43
- 1
- 3
- 35
- Hi, Meera
- 19
- 1
- 12
- 19
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students