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
Write a getter
easy10 minAdd a fullName getter that joins the first and last names.
- 2
Recalculate on read
easy10 minAdd an area getter, change the width, and show the new area.
- 3
Write a setter
easy10 minAdd a setter that splits a full name into two properties.
- 4
Validate in a setter
easy10 minReject a negative balance by storing zero instead.
- 5
Use a getter in a class
medium10 minAdd an area getter to a Circle class and show it rounded.
Find the bug(5)
- 1
Fix the getter joining the wrong way round
easy8 minThe names are being joined last first.
- 2
Fix the value cached instead of computed
easy8 minA plain property is worked out once and never updates.
- 3
Fix the setter with no parameter
easy8 minA setter must accept exactly one value.
- 4
Fix the validation skipped
easy8 minA negative balance is being stored instead of rejected.
- 5
Fix the missing get keyword
easy8 minWithout 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