Give the class a count field starting at zero and increase it once.
Acceptance criteria
- 1.#out should show 1.
JavaScript Tutorial · JavaScript Classes
These 10 tasks go with the JavaScript Class Fields 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 fields declare properties directly in the class body.” .
1.Declare a fieldexercise · easy · 10 min
Give the class a count field starting at zero and increase it once.
Passes when: #out should show 1.
2.Give each instance its own arrayexercise · easy · 10 min · practice plan
Push to one basket and report both lengths.
Passes when: #out should show 1 and 0.
3.Use a field in the constructorexercise · easy · 10 min · practice plan
Build a message from a field and a parameter.
Passes when: #out should show Hello Ada.
4.Override a fieldexercise · easy · 10 min · practice plan
Let the constructor replace the field default when a value is given.
Passes when: #out should show 5.
5.Use an arrow fieldexercise · medium · 10 min · practice plan
Make greet an arrow field so it keeps this when detached.
Passes when: #out should show Hello Ada.
6.Fix the field name that does not matchbug fix · easy · 8 min
The method increases a name that was never declared as a field.
Passes when: #out should show 1.
7.Fix the shared arraybug fix · easy · 8 min · practice plan
A static field is shared, so both baskets see the same items.
Passes when: #out should show 1 and 0.
8.Fix the missing field declarationbug fix · easy · 8 min · practice plan
The class declares no field, so the property does not exist.
Passes when: #out should show ready to go.
9.Fix the normal method losing thisbug fix · easy · 8 min · practice plan
A detached method needs an arrow field to keep its instance.
Passes when: #out should show Hello Ada.
10.Fix the default wiped by the constructorbug fix · easy · 8 min · practice plan
The constructor assigns even when no value was passed, losing the field's default.
Passes when: #out should show 1.
Between them these tasks cover Class fields, Defaults and Arrow fields.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.