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