Pass a function to another function and show what it returns.
Acceptance criteria
- 1.#out should show done.
JavaScript Tutorial · JavaScript Functions
These 10 tasks go with the JavaScript Callback 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: “A JavaScript callback function is a function passed to another function to be run later.” .
1.Pass a callbackexercise · easy · 10 min
Pass a function to another function and show what it returns.
Passes when: #out should show done.
2.A named callbackexercise · easy · 10 min · practice plan
Pass a named function to map to double each value, then join with commas.
Passes when: #out should show 2,4,6.
3.A callback with an argumentexercise · easy · 10 min · practice plan
Call the callback three times with the count, collecting the values.
Passes when: #out should show 1-2-3.
4.Filter with a callbackexercise · easy · 10 min · practice plan
Use filter with a callback to keep numbers above 4, then join with commas.
Passes when: #out should show 8,12.
5.Transform with a callbackexercise · medium · 10 min · practice plan
Write a process function that applies a callback to every item. Square them and join with commas.
Passes when: #out should show 1,4,9,16.
6.Fix the callback called too earlybug fix · easy · 8 min
The brackets run the function and pass its result instead.
Passes when: #out should show done.
7.Fix the wrong operationbug fix · easy · 8 min · practice plan
The callback adds 2 instead of doubling.
Passes when: #out should show 2,4,6.
8.Fix the ignored argumentbug fix · easy · 8 min · practice plan
The callback is given the count but never uses it.
Passes when: #out should show 1-2-3.
9.Fix the inverted filterbug fix · easy · 8 min · practice plan
The condition keeps the wrong numbers.
Passes when: #out should show 8,12.
10.Fix the callback that returns nothingbug fix · easy · 8 min · practice plan
Without a return the collected values are all undefined.
Passes when: #out should show 1,4,9,16.
Between them these tasks cover Callbacks, Passing functions and Callback arguments.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.