JavaScript Worksheet: If and For Loops
Code tip (Check, then count): if (condition) { … } else { … }: only one of the two parts runs. for (let i = 0; i < 5; i++) runs for i = 0, 1, 2, 3, 4 — 5 times. With i <= n it includes n; with i = i + 2 it goes up in twos.
NameDateTime takenScore ___ / 40
-
1.🔁 What does this code print?
let age = 22;
if (age > 14) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen -
2.🔁 How many numbers does this code print?
for (let i = 0; i < 6; i = i + 2) {
console.log(i);
} -
3.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 3; i++) {
total = total + i;
}
console.log(total); -
4.🔁 How many numbers does this code print?
for (let i = 0; i < 11; i = i + 3) {
console.log(i);
} -
5.🔁 How many numbers does this code print?
for (let i = 0; i < 8; i = i + 4) {
console.log(i);
} -
6.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 8; i++) {
total = total + i;
}
console.log(total); -
7.🔁 How many numbers does this code print?
for (let i = 0; i < 5; i = i + 3) {
console.log(i);
} -
8.🔁 What does this code print?
let score = 37;
if (score >= 10) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
9.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 8; i++) {
total = total + 7;
}
console.log(total); - 10.🔁 Which word runs code only when something is true? (a) for (b) log (c) if (d) let
-
11.🔁 What is the LAST number this code prints?
for (let i = 0; i < 17; i = i + 2) {
console.log(i);
} -
12.🔁 How many times does this code print “Hello”?
for (let i = 3; i < 7; i++) {
console.log("Hello");
} -
13.🔁 What does this code print?
let age = 9;
if (age >= 42) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
14.🔁 How many times does this code print “Hi”?
for (let i = 0; i < 2; i++) {
console.log("Hi");
} - 15.🔁 What does i++ do? (a) takes 1 from i (b) prints i (c) adds 1 to i (d) doubles i
- 16.🔁 Which loop repeats code a set number of times? (a) for (b) let (c) if (d) else
-
17.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 10; i++) {
total = total + 7;
}
console.log(total); - 18.🔁 When does the else part run? (a) always (b) never (c) when if is false (d) when if is true
-
19.🔁 What is the LAST number this code prints?
for (let i = 0; i < 5; i = i + 2) {
console.log(i);
} -
20.🔁 How many times does this code print “Hello”?
for (let i = 1; i < 5; i++) {
console.log("Hello");
} -
21.🔁 What does this code print?
let marks = 14;
if (marks >= 30) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
22.🔁 What does this code print?
let marks = 16;
if (marks >= 10) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Try again (b) Pass -
23.🔁 What does this code print?
let marks = 40;
if (marks > 11) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Try again (b) Pass -
24.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 4; i++) {
total = total + i;
}
console.log(total); -
25.🔁 What does this code print?
let age = 8;
if (age >= 22) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen -
26.🔁 How many times does this code print “Welcome”?
for (let i = 0; i <= 5; i++) {
console.log("Welcome");
} -
27.🔁 How many times does this code print “Hello”?
for (let i = 1; i <= 4; i++) {
console.log("Hello");
} -
28.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 9; i++) {
total = total + i;
}
console.log(total); -
29.🔁 What does this code print?
let marks = 25;
if (marks > 43) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
30.🔁 What does this code print?
let age = 27;
if (age >= 27) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
31.🔁 How many times does this code print “Welcome”?
for (let i = 0; i < 2; i++) {
console.log("Welcome");
} -
32.🔁 How many numbers does this code print?
for (let i = 0; i < 12; i = i + 3) {
console.log(i);
} -
33.🔁 How many times does this code print “Hello”?
for (let i = 0; i <= 9; i++) {
console.log("Hello");
} -
34.🔁 What does this code print?
let age = 10;
if (age < 13) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen -
35.🔁 How many times does this code print “Hi”?
for (let i = 1; i <= 5; i++) {
console.log("Hi");
} -
36.🔁 How many times does this code print “Welcome”?
for (let i = 0; i < 7; i++) {
console.log("Welcome");
} -
37.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 6; i++) {
total = total + i;
}
console.log(total); -
38.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 9; i++) {
total = total + 3;
}
console.log(total); -
39.🔁 What is the LAST number this code prints?
for (let i = 0; i < 6; i = i + 2) {
console.log(i);
} -
40.🔁 How many times does this code print “Well done”?
for (let i = 0; i < 6; i++) {
console.log("Well done");
}
Answer key
- teen
- 3
- 6
- 4
- 2
- 36
- 2
- You win
- 56
- if
- 16
- 4
- child
- 2
- adds 1 to i
- for
- 70
- when if is false
- 4
- 4
- Try again
- Pass
- Pass
- 10
- child
- 6
- 4
- 45
- Try again
- teen
- 2
- 4
- 10
- teen
- 5
- 7
- 21
- 27
- 4
- 6
Free html and css lessons, practice and worksheets at talentjr.in/html-css-for-kids