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