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.🔁 How many times does this code print “Welcome”?
for (let i = 2; i <= 5; i++) {
console.log("Welcome");
} - 2.🔁 Which word runs code only when something is true? (a) log (b) let (c) if (d) for
-
3.🔁 What does this code print?
let marks = 18;
if (marks < 17) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again - 4.🔁 When does the else part run? (a) never (b) when if is true (c) always (d) when if is false
- 5.🔁 Which loop repeats code a set number of times? (a) let (b) for (c) if (d) else
-
6.🔁 What does this code print?
let age = 45;
if (age > 25) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen -
7.🔁 What does this code print?
let temp = 26;
if (temp > 5) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
8.🔁 How many times does this code print “Welcome”?
for (let i = 2; i <= 10; i++) {
console.log("Welcome");
} -
9.🔁 How many times does this code print “Hi”?
for (let i = 1; i < 10; i++) {
console.log("Hi");
} -
10.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 3; i++) {
total = total + 9;
}
console.log(total); -
11.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 8; i++) {
total = total + i;
}
console.log(total); -
12.🔁 What does this code print?
let score = 15;
if (score >= 24) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
13.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 10; i++) {
total = total + i;
}
console.log(total); -
14.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 9; i++) {
total = total + i;
}
console.log(total); -
15.🔁 What is the LAST number this code prints?
for (let i = 0; i < 14; i = i + 2) {
console.log(i);
} -
16.🔁 What does this code print?
let temp = 31;
if (temp > 44) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Cool (b) Hot -
17.🔁 How many times does this code print “Well done”?
for (let i = 1; i < 9; i++) {
console.log("Well done");
} -
18.🔁 What does this code print?
let age = 27;
if (age >= 37) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
19.🔁 How many times does this code print “Well done”?
for (let i = 0; i < 3; i++) {
console.log("Well done");
} -
20.🔁 What does this code print?
let score = 10;
if (score < 42) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
21.🔁 How many numbers does this code print?
for (let i = 0; i < 12; i = i + 2) {
console.log(i);
} -
22.🔁 What does this code print?
let marks = 11;
if (marks > 23) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
23.🔁 What does this code print?
let age = 46;
if (age < 13) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
24.🔁 What does this code print?
let score = 22;
if (score > 9) {
console.log("You win");
} else {
console.log("Play again");
} (a) You win (b) Play again -
25.🔁 What does this code print?
let age = 25;
if (age >= 44) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
26.🔁 What does this code print?
let marks = 9;
if (marks < 15) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again - 27.🔁 What does i++ do? (a) doubles i (b) takes 1 from i (c) prints i (d) adds 1 to i
-
28.🔁 What does this code print?
let score = 19;
if (score < 30) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
29.🔁 What is the LAST number this code prints?
for (let i = 0; i < 8; i = i + 4) {
console.log(i);
} -
30.🔁 What does this code print?
let score = 23;
if (score < 12) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
31.🔁 How many times does this code print “Well done”?
for (let i = 0; i < 5; i++) {
console.log("Well done");
} -
32.🔁 What is the LAST number this code prints?
for (let i = 0; i < 11; i = i + 2) {
console.log(i);
} -
33.🔁 How many times does this code print “Well done”?
for (let i = 3; i < 11; i++) {
console.log("Well done");
} -
34.🔁 How many numbers does this code print?
for (let i = 0; i < 15; i = i + 4) {
console.log(i);
} -
35.🔁 What does this code print?
let age = 9;
if (age > 18) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
36.🔁 What does this code print?
let marks = 45;
if (marks < 38) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
37.🔁 How many numbers does this code print?
for (let i = 0; i < 20; i = i + 3) {
console.log(i);
} -
38.🔁 How many numbers does this code print?
for (let i = 0; i < 9; i = i + 3) {
console.log(i);
} -
39.🔁 What is the LAST number this code prints?
for (let i = 0; i < 13; i = i + 3) {
console.log(i);
} -
40.🔁 How many numbers does this code print?
for (let i = 0; i < 13; i = i + 2) {
console.log(i);
}
Answer key
- 4
- if
- Try again
- when if is false
- for
- teen
- Hot
- 9
- 9
- 27
- 36
- Play again
- 55
- 45
- 12
- Cool
- 8
- child
- 3
- You win
- 6
- Try again
- child
- You win
- child
- Pass
- adds 1 to i
- You win
- 4
- Play again
- 5
- 10
- 8
- 4
- child
- Try again
- 7
- 3
- 12
- 7
Free html and css lessons, practice and worksheets at talentjr.in/html-css-for-kids