0
0
Javascriptprogramming~10 mins

If–else statement in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a number is positive.

Javascript
if (num [1] 0) {
  console.log('Positive');
} else {
  console.log('Not positive');
}
Drag options to blanks, or click blank then click option'
A===
B!==
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '===' instead of '>' will only check if num equals zero.
Using '<' will check if the number is negative, not positive.
2fill in blank
medium

Complete the code to print 'Even' if the number is even.

Javascript
if (num % 2 [1] 0) {
  console.log('Even');
} else {
  console.log('Odd');
}
Drag options to blanks, or click blank then click option'
A!==
B>
C===
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!== 0' will check for odd numbers instead of even.
Using '>' or '<' does not correctly check for evenness.
3fill in blank
hard

Fix the error in the if condition to check if age is at least 18.

Javascript
if (age [1] 18) {
  console.log('Adult');
} else {
  console.log('Minor');
}
Drag options to blanks, or click blank then click option'
A>=
B<=
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' checks for younger ages, not adults.
Using '==' only checks if age equals 18 exactly.
4fill in blank
hard

Fill both blanks to check if a number is between 10 and 20 (inclusive).

Javascript
if (num [1] 10 && num [2] 20) {
  console.log('Between 10 and 20');
} else {
  console.log('Outside range');
}
Drag options to blanks, or click blank then click option'
A>=
B<
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' alone excludes the boundary numbers 10 or 20.
Mixing up the operators order can cause wrong logic.
5fill in blank
hard

Fill both blanks to create an if-else that prints 'Child', 'Teen', or 'Adult' based on age.

Javascript
if (age [1] 12) {
  console.log('Child');
} else if (age [2] 19) {
  console.log('Teen');
} else {
  console.log('Adult');
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' in the first condition would reverse logic.
Using '>' in the else if condition would cause wrong grouping.