Add an area getter that multiplies the sides.
Acceptance criteria
- 1.#out should show 20.
JavaScript Tutorial · JavaScript Classes
These 10 tasks go with the JavaScript Class Getters and Setters chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Class getters and setters let a method be used like a plain property.” .
1.Write a getterexercise · easy · 10 min
Add an area getter that multiplies the sides.
Passes when: #out should show 20.
2.Recalculate on readexercise · easy · 10 min · practice plan
Change the width and show the area updates by itself.
Passes when: #out should show 50.
3.Make a property read onlyexercise · easy · 10 min · practice plan
Expose a private balance with a getter and no setter.
Passes when: #out should show 100.
4.Validate in a setterexercise · easy · 10 min · practice plan
Refuse a negative age by storing zero instead.
Passes when: #out should show 0.
5.Split a value in a setterexercise · medium · 10 min · practice plan
Set fullName and have it update both parts.
Passes when: #out should show Grace Hopper.
6.Fix the missing get keywordbug fix · easy · 8 min
Without get the property holds a function rather than its result.
Passes when: #out should show 20.
7.Fix the value worked out oncebug fix · easy · 8 min · practice plan
A stored property does not update when the width changes.
Passes when: #out should show 50.
8.Fix the property left writablebug fix · easy · 8 min · practice plan
A public field can be overwritten from outside.
Passes when: #out should show 100.
9.Fix the setter that skips validationbug fix · easy · 8 min · practice plan
The negative value is being stored as it is.
Passes when: #out should show 0.
10.Fix the setter with no parameterbug fix · easy · 8 min · practice plan
A setter must take exactly one value.
Passes when: #out should show Grace.
Between them these tasks cover Getters, Setters and Read-only properties.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.