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