JavaScript Class Fields — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Class Fields 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
Declare a field
easy10 minGive the class a count field starting at zero and increase it once.
- 2
Give each instance its own array
easy10 minPush to one basket and report both lengths.
- 3
Use a field in the constructor
easy10 minBuild a message from a field and a parameter.
- 4
Override a field
easy10 minLet the constructor replace the field default when a value is given.
- 5
Use an arrow field
medium10 minMake greet an arrow field so it keeps this when detached.
Find the bug(5)
- 1
Fix the field name that does not match
easy8 minThe method increases a name that was never declared as a field.
- 2
Fix the shared array
easy8 minA static field is shared, so both baskets see the same items.
- 3
Fix the missing field declaration
easy8 minThe class declares no field, so the property does not exist.
- 4
Fix the normal method losing this
easy8 minA detached method needs an arrow field to keep its instance.
- 5
Fix the default wiped by the constructor
easy8 minThe constructor assigns even when no value was passed, losing the field's default.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace