Skip to content

Instantly share code, notes, and snippets.

@thm-design
Created March 28, 2016 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thm-design/f2e9998386b9addcecab to your computer and use it in GitHub Desktop.
Save thm-design/f2e9998386b9addcecab to your computer and use it in GitHub Desktop.
Simple recursion example
/*
Make 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
*/
const factorial = (n) => {
if (n === 0) return 1;
return n * factorial(n-1);
}
// Jasmine 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