JavaScript if Statement — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript if Statement lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Show a message when true
easy10 minIf age is 20, show "adult".
- 2
Test a string value
easy10 minIf the role is "admin", show "allowed".
- 3
Check an empty list
easy10 minIf the cart has no items, show "empty".
- 4
Combine two conditions
easy10 minIf the user is logged in AND verified, show "welcome".
- 5
Guard against a missing value
medium10 minOnly show the name when it is present; here it is "Sam".
Find the bug(5)
- 1
Fix the assignment in the condition
easy8 minA single = makes this condition always run.
- 2
Fix the inverted condition
easy8 minThe branches sit on the wrong side of the test, so a 20-year-old is labelled a minor.
- 3
Fix the loose equality
easy8 minLoose equality lets a string match a number.
- 4
Fix the misplaced semicolon
easy8 minA semicolon right after if() ends the statement early.
- 5
Fix the wrong logical operator
easy8 minOR is used where both conditions must hold.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace