JavaScript Pure Functions — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Pure Functions lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Write a pure function
easy10 minWrite a pure add function and call it twice with the same values, joining the answers.
- 2
Take everything as a parameter
easy10 minPass the tax rate in rather than reading an outside variable. Show 100 at 1.2.
- 3
Return a new array
easy10 minAdd a value without changing the original array. Show the original length.
- 4
Copy before sorting
easy10 minSort a copy so the original order is kept. Show the original joined by commas.
- 5
A pure total
medium10 minAdd up a list with reduce without changing it, and show the total.
Find the bug(5)
- 1
Fix the outside variable
easy8 minReading an outside rate makes the answer change unexpectedly.
- 2
Fix the array that was changed
easy8 minpush changes the array that was passed in.
- 3
Fix the sort that reorders the original
easy8 minsort changes the array it is called on.
- 4
Fix the counter side effect
easy8 minThe function changes a variable outside itself.
- 5
Fix the object that was mutated
easy8 minThe function changes the object it was given.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace