Write a Person constructor and build one with the name Ada.
Acceptance criteria
- 1.#out should show Ada.
JavaScript Tutorial · JavaScript Objects
These 10 tasks go with the JavaScript Object Constructors 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: “A JavaScript constructor function builds many objects with the same shape.” .
1.Write a constructorexercise · easy · 10 min
Write a Person constructor and build one with the name Ada.
Passes when: #out should show Ada.
2.Store two propertiesexercise · easy · 10 min · practice plan
Build a Car with a brand and year and show both.
Passes when: #out should show Toyota 2021.
3.Build two separate objectsexercise · easy · 10 min · practice plan
Build two cars and show that they hold different brands.
Passes when: #out should show Toyota Honda.
4.Add a prototype methodexercise · easy · 10 min · practice plan
Put a greet method on the prototype and call it.
Passes when: #out should show Hi Grace.
5.Use a class insteadexercise · medium · 10 min · practice plan
Write the same thing as a class and call its method.
Passes when: #out should show Hey Alan.
6.Fix the missing new keywordbug fix · easy · 8 min
Without new the constructor builds nothing and returns undefined.
Passes when: #out should show Ada.
7.Fix the parameter never storedbug fix · easy · 8 min · practice plan
The constructor takes a name but never puts it on the object.
Passes when: #out should show Toyota.
8.Fix the shared objectbug fix · easy · 8 min · practice plan
Returning one fixed object means every car is the same one.
Passes when: #out should show Toyota Honda.
9.Fix the method on the wrong placebug fix · easy · 8 min · practice plan
The method was put on the constructor itself, not its prototype.
Passes when: #out should show Hi Grace.
10.Fix the class constructor named wronglybug fix · easy · 8 min · practice plan
The setup method must be called constructor.
Passes when: #out should show Hey Alan.
Between them these tasks cover Constructors, new and Prototypes.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.