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