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