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