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