Write an arrow function that doubles a number. Show it for 25.
Acceptance criteria
- 1.#out should show 50.
JavaScript Tutorial · JavaScript Functions
These 10 tasks go with the JavaScript Arrow Functions 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: “JavaScript arrow functions are a shorter way to write a function expression.” .
1.Write an arrow functionexercise · easy · 10 min
Write an arrow function that doubles a number. Show it for 25.
Passes when: #out should show 50.
2.Use an implicit returnexercise · easy · 10 min · practice plan
Write the same doubling function in one line with no braces. Show it for 33.
Passes when: #out should show 66.
3.Two parametersexercise · easy · 10 min · practice plan
Write an arrow function that adds two numbers. Show 8 plus 9.
Passes when: #out should show 17.
4.An arrow as a callbackexercise · easy · 10 min · practice plan
Use an arrow function with map to square each number, then join with commas.
Passes when: #out should show 1,4,9.
5.Return an objectexercise · medium · 10 min · practice plan
Return an object from a one-line arrow function and show its name.
Passes when: #out should show Ada.
6.Fix the missing arrowbug fix · easy · 8 min
An arrow function needs the arrow between the parameters and the body.
Passes when: #out should show 50.
7.Fix the braces without returnbug fix · easy · 8 min · practice plan
With braces the function must return explicitly.
Passes when: #out should show 66.
8.Fix the missing bracketsbug fix · easy · 8 min · practice plan
With two parameters the round brackets are required.
Passes when: #out should show 17.
9.Fix the object read as a bodybug fix · easy · 8 min · practice plan
Without round brackets the braces are treated as a function body.
Passes when: #out should show Ada.
10.Fix the arrow used as a methodbug fix · easy · 8 min · practice plan
An arrow function has no this, so it cannot see the object.
Passes when: #out should show Hi Ada.
Between them these tasks cover Arrow syntax, Implicit return and Arrow functions and this.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.