Store a name and an age, then report them together.
Acceptance criteria
- 1.#out should show Ada is 36.
JavaScript Tutorial · JavaScript Classes
These 10 tasks go with the JavaScript Class Constructor 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: “The constructor runs when an instance is created and sets up its properties.” .
1.Set two propertiesexercise · easy · 10 min
Store a name and an age, then report them together.
Passes when: #out should show Ada is 36.
2.Give a default valueexercise · easy · 10 min · practice plan
Default the name to unknown when none is passed.
Passes when: #out should show unknown.
3.Work out a derived valueexercise · easy · 10 min · practice plan
Store the area worked out from the width and height.
Passes when: #out should show 20.
4.Reject a bad valueexercise · easy · 10 min · practice plan
Throw when the balance is negative and report the message.
Passes when: #out should show balance cannot be negative.
5.Use a class with no constructorexercise · medium · 10 min · practice plan
Create an instance of an empty class and report its type.
Passes when: #out should show object.
6.Fix the misspelled constructorbug fix · easy · 8 min
The setup method must be named constructor exactly.
Passes when: #out should show Ada.
7.Fix the default that never appliesbug fix · easy · 8 min · practice plan
The default belongs on the parameter, not inside a condition on the wrong value.
Passes when: #out should show unknown.
8.Fix the parameters in the wrong orderbug fix · easy · 8 min · practice plan
The arguments are being stored the wrong way round.
Passes when: #out should show Ada is 36.
9.Fix the validation that never firesbug fix · easy · 8 min · practice plan
The condition checks the wrong way, so the bad value is accepted.
Passes when: #out should show balance cannot be negative.
10.Fix the derived value never storedbug fix · easy · 8 min · practice plan
The area is worked out but not kept on the instance.
Passes when: #out should show 20.
Between them these tasks cover constructor, Default parameters and Validation.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.