Add a static double method and call it on the class.
Acceptance criteria
- 1.#out should show 10.
JavaScript Tutorial · JavaScript Classes
These 10 tasks go with the JavaScript Static Members 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: “Static members belong to the class itself rather than to any instance.” .
1.Write a static methodexercise · easy · 10 min
Add a static double method and call it on the class.
Passes when: #out should show 10.
2.Add a static propertyexercise · easy · 10 min · practice plan
Store a version on the class and report it.
Passes when: #out should show 1.0.
3.Count instancesexercise · easy · 10 min · practice plan
Count how many users have been created.
Passes when: #out should show 2.
4.Write a factory methodexercise · easy · 10 min · practice plan
Add a static origin method that returns a point at zero.
Passes when: #out should show 0,0.
5.Show instances cannot see staticsexercise · medium · 10 min · practice plan
Report the type of a static method read from an instance.
Passes when: #out should show undefined.
6.Fix the static called on an instancebug fix · easy · 8 min
A static method belongs to the class, not to instances.
Passes when: #out should show 10.
7.Fix the missing static keywordbug fix · easy · 8 min · practice plan
Without static the method sits on instances instead of the class.
Passes when: #out should show 1.0.
8.Fix the counter stored per instancebug fix · easy · 8 min · practice plan
An instance field gives each object its own count.
Passes when: #out should show 2.
9.Fix the factory that returns nothingbug fix · easy · 8 min · practice plan
The static method builds a point but never returns it.
Passes when: #out should show 0,0.
10.Fix the static reading an instance propertybug fix · easy · 8 min · practice plan
A static method has no instance, so it cannot read this.name.
Passes when: #out should show a class method.
Between them these tasks cover static, Factory methods and Class properties.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.