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