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