The browser storage localStorage is not available. Either your browser does not support it or you have disabled it or the maximum memory size is exceeded. Without localStorage your solutions will not be stored.

Recursion

Functions can call themselves. This is called recursion. The best known example is the calculation of the factorial:
function factorial(n) {
  if (n === 0) {
    return 1;
  }
  return factorial(n - 1) * n;
}
The factorial of a natural number is the product of all natural numbers (excluding zero) less than or equal to that number. It is marked by a trailing exclamation mark: n! = 1 * 2 * 3 ... (n-1) * n. You can see that you can use the factorial of n-1 to calculate the factorial of n: n! = (n-1)! * n. The code above is the implementation of this equation. To calculate the factorial of n, you have to calculate the factorial of n-1 and multiply the result by n. So the function calls itself repeatedly with a value reduced by 1. Now you need a termination condition so that the recursion does not continue infinitely. This termination condition is implemented by the if condition. If n has reached 0, the function no longer calls itself, but the result, namely 0! = 1, is returned directly.

Exercise

Write a function reverse that reverses the order of the characters in a string. The function should be recursive.

Example: reverse('live') should return 'evil'.

loving