Skip to main content

JavaScript Recursion — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Recursion 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

    Count down

    easy10 min

    Count down from 3 to 1 with recursion, collecting the numbers joined by a dash.

  2. 2

    Factorial

    easy10 min

    Work out factorial of 5 with recursion.

  3. 3

    Add up an array

    easy10 min

    Add every number in the array using recursion.

  4. 4

    Count nested items

    easy10 min

    Count every item in a nested list, however deep it goes.

  5. 5

    Reverse a string

    medium10 min

    Reverse the word stream using recursion.

Find the bug(5)

  1. 1

    Fix the wrong base case

    easy8 min

    The base case returns the wrong starting value.

  2. 2

    Fix the value that never shrinks

    easy8 min

    The recursive call passes the same value, so nothing progresses.

  3. 3

    Fix the missing recursive call

    easy8 min

    Nested lists are counted as one item instead of being opened up.

  4. 4

    Fix the reversed join order

    easy8 min

    The characters are put back together the wrong way round.

  5. 5

    Fix the base case checked too late

    easy8 min

    The list is read before checking whether it is empty.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace