Skip to content

Instantly share code, notes, and snippets.

@leandroh
Created July 4, 2016 20:40
Show Gist options
  • Save leandroh/78e6c52c0267ac846be8f3e2b04d9c46 to your computer and use it in GitHub Desktop.
Save leandroh/78e6c52c0267ac846be8f3e2b04d9c46 to your computer and use it in GitHub Desktop.
Test recursive factorial function in JavaScript.
const assert = require('assert');
function factorial(n) {
if (n > 1) {
return n * factorial(n - 1);
}
return 1;
}
assert.equal(factorial(1), 1);
assert.equal(factorial(2), 2);
assert.equal(factorial(3), 6);
assert.equal(factorial(4), 24);
assert.equal(factorial(5), 120);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment