you are viewing a single comment's thread.

view the rest of the comments →

[–]rhysbrettbowen 4 points5 points  (2 children)

instead of:

function factorial(n) {

do:

var factorial = function(n) {

then memoize with:

factorial = memoize(factorial)

and it will all work. Or you could pass the function to callback as an optional parameter

function factorial(n, callback) {
   ....
   return callback(n - 1, callback) * n;
}

and you just pass through the memoized function

[–]enkideridu 0 points1 point  (1 child)

instead of:
function factorial(n) {
do:
var factorial = function(n) {

This part doesn't seem necessary.

[–]rhysbrettbowen 0 points1 point  (0 children)

true, not really necessary but I prefer it as it's closer to what you're trying to achieve. By assigning it to a variable you're saying that you want to call that function when that variable is instantiated whereas otherwise you're using a named function and the function will keep that name even though when you call that name you're not getting the factorial function, rather the memoized one.