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