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