Joined September 2013
·

Jason Roos

Greater Los Angeles Area
·

Your example doesn't work because you are testing for a property of the cache object before the cache object has been defined. Also, you shouldn't check for a value of null, since null is a value that must be explicitly assigned beforehand. You could check for undefined or, better, just do an implicit comparison, since both null and undefined evaluate to true. If you want to do it your way, the beginning of your function should look like this:


function isPrime(num) { if (!isPrime.cache) { var isPrime.cache = {}; } else if (isPrime.cache[num]) { return isPrime.cache[num]; } ... } </code> </pre> BETTER YET, why not omit the whole cache property, and just make it a property of the function object itself? function isPrime(num) { if (isPrime[num]) { return isPrime[num]; } ... } </code> </pre>
Achievements
1 Karma
0 Total ProTip Views