Skip to main content

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. 1

    Set two properties

    easy10 min

    Store a name and an age, then report them together.

  2. 2

    Give a default value

    easy10 min

    Default the name to unknown when none is passed.

  3. 3

    Work out a derived value

    easy10 min

    Store the area worked out from the width and height.

  4. 4

    Reject a bad value

    easy10 min

    Throw when the balance is negative and report the message.

  5. 5

    Use a class with no constructor

    medium10 min

    Create an instance of an empty class and report its type.

Find the bug(5)

  1. 1

    Fix the misspelled constructor

    easy8 min

    The setup method must be named constructor exactly.

  2. 2

    Fix the default that never applies

    easy8 min

    The default belongs on the parameter, not inside a condition on the wrong value.

  3. 3

    Fix the parameters in the wrong order

    easy8 min

    The arguments are being stored the wrong way round.

  4. 4

    Fix the validation that never fires

    easy8 min

    The condition checks the wrong way, so the bad value is accepted.

  5. 5

    Fix the derived value never stored

    easy8 min

    The 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