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