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