Skip to main content

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. 1

    Write a pure function

    easy10 min

    Write a pure add function and call it twice with the same values, joining the answers.

  2. 2

    Take everything as a parameter

    easy10 min

    Pass the tax rate in rather than reading an outside variable. Show 100 at 1.2.

  3. 3

    Return a new array

    easy10 min

    Add a value without changing the original array. Show the original length.

  4. 4

    Copy before sorting

    easy10 min

    Sort a copy so the original order is kept. Show the original joined by commas.

  5. 5

    A pure total

    medium10 min

    Add up a list with reduce without changing it, and show the total.

Find the bug(5)

  1. 1

    Fix the outside variable

    easy8 min

    Reading an outside rate makes the answer change unexpectedly.

  2. 2

    Fix the array that was changed

    easy8 min

    push changes the array that was passed in.

  3. 3

    Fix the sort that reorders the original

    easy8 min

    sort changes the array it is called on.

  4. 4

    Fix the counter side effect

    easy8 min

    The function changes a variable outside itself.

  5. 5

    Fix the object that was mutated

    easy8 min

    The 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