Last active
December 20, 2015 00:09
-
-
Save justinobney/6039393 to your computer and use it in GitHub Desktop.
Simple JS Factorial Calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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