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