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