Skip to main content

JavaScript Getters and Setters — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript 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 a fullName getter that joins the first and last names.

  2. 2

    Recalculate on read

    easy10 min

    Add an area getter, change the width, and show the new area.

  3. 3

    Write a setter

    easy10 min

    Add a setter that splits a full name into two properties.

  4. 4

    Validate in a setter

    easy10 min

    Reject a negative balance by storing zero instead.

  5. 5

    Use a getter in a class

    medium10 min

    Add an area getter to a Circle class and show it rounded.

Find the bug(5)

  1. 1

    Fix the getter joining the wrong way round

    easy8 min

    The names are being joined last first.

  2. 2

    Fix the value cached instead of computed

    easy8 min

    A plain property is worked out once and never updates.

  3. 3

    Fix the setter with no parameter

    easy8 min

    A setter must accept exactly one value.

  4. 4

    Fix the validation skipped

    easy8 min

    A negative balance is being stored instead of rejected.

  5. 5

    Fix the missing get keyword

    easy8 min

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

Ready to write the code?

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

Open the workspace