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
Write a getter
easy10 minAdd an area getter that multiplies the sides.
- 2
Recalculate on read
easy10 minChange the width and show the area updates by itself.
- 3
Make a property read only
easy10 minExpose a private balance with a getter and no setter.
- 4
Validate in a setter
easy10 minRefuse a negative age by storing zero instead.
- 5
Split a value in a setter
medium10 minSet fullName and have it update both parts.
Find the bug(5)
- 1
Fix the missing get keyword
easy8 minWithout get the property holds a function rather than its result.
- 2
Fix the value worked out once
easy8 minA stored property does not update when the width changes.
- 3
Fix the property left writable
easy8 minA public field can be overwritten from outside.
- 4
Fix the setter that skips validation
easy8 minThe negative value is being stored as it is.
- 5
Fix the setter with no parameter
easy8 minA 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