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