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