Store a doubling function in a variable and show the result for 16.
Acceptance criteria
- 1.#out should show 32.
JavaScript Tutorial · JavaScript Functions
These 10 tasks go with the JavaScript Function Expressions 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 function expression stores a function inside a variable.” .
1.Store a function in a variableexercise · easy · 10 min
Store a doubling function in a variable and show the result for 16.
Passes when: #out should show 32.
2.An anonymous functionexercise · easy · 10 min · practice plan
Store an anonymous function that returns the word Ready.
Passes when: #out should show Ready.
3.Pass a function to mapexercise · easy · 10 min · practice plan
Pass a function expression to map to triple each number, then join with commas.
Passes when: #out should show 3,6,9.
4.A function on an objectexercise · easy · 10 min · practice plan
Give an object an add method and show 2 plus 5.
Passes when: #out should show 7.
5.A named function expressionexercise · medium · 10 min · practice plan
Use a named function expression to work out factorial of 5.
Passes when: #out should show 120.
6.Fix the missing assignmentbug fix · easy · 8 min
The function is never stored, so the variable holds nothing.
Passes when: #out should show 32.
7.Fix the call before the definitionbug fix · easy · 8 min · practice plan
A function expression cannot be used above the line that creates it.
Passes when: #out should show Ready.
8.Fix the wrong function passedbug fix · easy · 8 min · practice plan
The doubling function was passed to map instead of the tripling one.
Passes when: #out should show 3,6,9.
9.Fix the wrong method namebug fix · easy · 8 min · practice plan
The object has no method by that name.
Passes when: #out should show 7.
10.Fix the missing base casebug fix · easy · 8 min · practice plan
Without a stopping point the recursion never ends.
Passes when: #out should show 120.
Between them these tasks cover Function expressions, Anonymous functions and Functions as values.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.