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