Skip to content

Instantly share code, notes, and snippets.

@justinobney
Last active December 20, 2015 00:09
Show Gist options
  • Save justinobney/6039393 to your computer and use it in GitHub Desktop.
Save justinobney/6039393 to your computer and use it in GitHub Desktop.
Simple JS Factorial Calculator
(function () {
'use strict';
var cache = {};
function factorial(number) {
var keyResult;
var cacheKey = number.toString();
console.log('Calculating factorial for ' + number);
if (cache[cacheKey]) {
console.log('Found answer in cache..');
return cache[cacheKey];
} else {
if (number == 0 || number == 1)
return number
else {
keyResult = number + factorial(number - 1)
cache[cacheKey] = keyResult
}
return keyResult
}
}
factorial(10);
factorial(5);
factorial(10);
factorial(13);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment