Add a greet method that uses the name on the object.
Acceptance criteria
- 1.#out should show Hello Ada.
JavaScript Tutorial · JavaScript Objects
These 10 tasks go with the JavaScript Object Methods 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 object method is a function stored as a property of an object.” .
1.Write a methodexercise · easy · 10 min
Add a greet method that uses the name on the object.
Passes when: #out should show Hello Ada.
2.Calculate from propertiesexercise · easy · 10 min · practice plan
Write an area method that multiplies the width and height.
Passes when: #out should show 20.
3.Take a parameterexercise · easy · 10 min · practice plan
Write a deposit method that adds to the balance and returns it.
Passes when: #out should show 150.
4.Call another methodexercise · easy · 10 min · practice plan
Have one method call another through this.
Passes when: #out should show Count is 1.
5.Add a method laterexercise · medium · 10 min · practice plan
Add a method to an existing object and call it.
Passes when: #out should show Hello Grace.
6.Fix the missing thisbug fix · easy · 8 min
Without this the method cannot see its own property.
Passes when: #out should show Hello Ada.
7.Fix the arrow used as a methodbug fix · easy · 8 min · practice plan
An arrow function has no this of its own, so the name is lost.
Passes when: #out should show Hi Grace.
8.Fix the method not calledbug fix · easy · 8 min · practice plan
Without brackets the function itself is shown, not its result.
Passes when: #out should show Hello Ada.
9.Fix the method with no returnbug fix · easy · 8 min · practice plan
The method works out the answer but never gives it back.
Passes when: #out should show 20.
10.Fix the balance never storedbug fix · easy · 8 min · practice plan
The method returns the new total but does not save it.
Passes when: #out should show 150.
Between them these tasks cover Methods, this and Shorthand syntax.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.