Skip to content

Instantly share code, notes, and snippets.

@ian-bartholomew
Last active March 1, 2017 06:45
Show Gist options
  • Save ian-bartholomew/5c03b2130ba1f94c42abd85e1037c3d7 to your computer and use it in GitHub Desktop.
Save ian-bartholomew/5c03b2130ba1f94c42abd85e1037c3d7 to your computer and use it in GitHub Desktop.
Recursive factorial function
/*
A function that computes a factorial recursively.
A factorial is when you take a number n and multiply by each preceding integer until you hit one.
n * (n-1) * (n-2) ... * 3 * 2 * 1
Call the function factorial
factorial(1) = 1
factorial(2) = 2
factorial(3) = 6
*/
function factorial(n) {
if (n < 2) return 1;
return n * factorial(n-1);
}
// unit tests
// do not modify the below code
describe('factorial', function() {
it('should do factorials', () => {
expect(factorial(1)).toEqual(1);
expect(factorial(2)).toEqual(2);
expect(factorial(3)).toEqual(6);
expect(factorial(10)).toEqual(3628800);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment