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