Created
June 29, 2012 16:04
-
-
Save bdtomlin/3018831 to your computer and use it in GitHub Desktop.
HomeWork 2
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.prototype.cached = function(){ | |
var that = this; | |
var cache = {}; | |
return function(arg){ | |
if(arg in cache){ | |
return cache[arg]; | |
}else{ | |
cache[arg] = that(arg); | |
return cache[arg]; | |
} | |
}; | |
}; | |
function isPrime(num){ | |
console.log('not in cache'); | |
var prime = num != 1; | |
for ( var i = 2; i < num; i++ ) { | |
if ( num % i == 0 ) { | |
prime = false; | |
break; | |
} | |
} | |
return prime; | |
} | |
cachedIsPrime = isPrime.cached(); | |
console.log(cachedIsPrime(11)); | |
console.log(cachedIsPrime(11)); | |
console.log(cachedIsPrime(11)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment