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