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