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