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