Skip to main content

JavaScript Class Getters and Setters — exercises and bug fixes

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

    easy10 min

    Add an area getter that multiplies the sides.

  2. 2

    Recalculate on read

    easy10 min

    Change the width and show the area updates by itself.

  3. 3

    Make a property read only

    easy10 min

    Expose a private balance with a getter and no setter.

  4. 4

    Validate in a setter

    easy10 min

    Refuse a negative age by storing zero instead.

  5. 5

    Split a value in a setter

    medium10 min

    Set fullName and have it update both parts.

Find the bug(5)

  1. 1

    Fix the missing get keyword

    easy8 min

    Without get the property holds a function rather than its result.

  2. 2

    Fix the value worked out once

    easy8 min

    A stored property does not update when the width changes.

  3. 3

    Fix the property left writable

    easy8 min

    A public field can be overwritten from outside.

  4. 4

    Fix the setter that skips validation

    easy8 min

    The negative value is being stored as it is.

  5. 5

    Fix the setter with no parameter

    easy8 min

    A setter must take exactly one value.

Ready to write the code?

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

Open the workspace