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