JavaScript Class Constructor — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Class Constructor 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
Set two properties
easy10 minStore a name and an age, then report them together.
- 2
Give a default value
easy10 minDefault the name to unknown when none is passed.
- 3
Work out a derived value
easy10 minStore the area worked out from the width and height.
- 4
Reject a bad value
easy10 minThrow when the balance is negative and report the message.
- 5
Use a class with no constructor
medium10 minCreate an instance of an empty class and report its type.
Find the bug(5)
- 1
Fix the misspelled constructor
easy8 minThe setup method must be named constructor exactly.
- 2
Fix the default that never applies
easy8 minThe default belongs on the parameter, not inside a condition on the wrong value.
- 3
Fix the parameters in the wrong order
easy8 minThe arguments are being stored the wrong way round.
- 4
Fix the validation that never fires
easy8 minThe condition checks the wrong way, so the bad value is accepted.
- 5
Fix the derived value never stored
easy8 minThe area is worked out but not kept on the instance.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace