Skip to main content

JavaScript Class Methods — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Class Methods 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

    Write a method with a parameter

    easy10 min

    Add a deposit method that increases the balance and returns it.

  2. 2

    Call another method

    easy10 min

    Have total call tax through this.

  3. 3

    Show methods are shared

    easy10 min

    Report whether two instances share the same method function.

  4. 4

    Chain method calls

    easy10 min

    Return this from up so the calls can be chained.

  5. 5

    Bind a detached method

    medium10 min

    Pull the method out and bind it so it still works.

Find the bug(5)

  1. 1

    Fix the method with no return

    easy8 min

    The method works out the answer but never gives it back.

  2. 2

    Fix the sibling method called without this

    easy8 min

    A method must be reached through this.

  3. 3

    Fix the chain that returns a value

    easy8 min

    up must return this for chaining to work.

  4. 4

    Fix the detached method

    easy8 min

    Taking a method out of its object loses this.

  5. 5

    Fix the method defined in the constructor

    easy8 min

    A method belongs in the class body so instances share it.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace