3

I am trying to write a memoization function, but keep getting the following error.

Error - "TypeError: getNthFibonacciNo is not a function
    at dabebimaya.js:28:38
    at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:13924
    at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:10866"

How can I find this error in my code? I have tried googling the error with no avail. Please point out any additional errors too if possible.

function memoize(fn) {
  var cache = {};
  if (cache[arguments[0]]!==undefined) {
    return cache[arguments[0]];
  }  
  else {
    var value = fn.apply(this, arguments);
    cache[arguments[0]] = value;  
    return value;
  }
}

var getNthFibonacciNo = memoize(function(n){
    //1,1,2,3,5,8,13,21,34
    if(i<=2)
      return 1;

    var fib = [0,1,1];
    for(var i=3;i<=n;i++) {
      fib[i] = fib[i-2]+fib[i-1];
    }

    return fib[n];
});

console.log(getNthFibonacciNo(7));
0

2 Answers 2

3

Your memoize function isn't returning a function.

function memoize(fn) {
  var cache = {};
  return function() {
    if (cache[arguments[0]]!==undefined) {
      return cache[arguments[0]];
    }  
    else {
      var value = fn.apply(this, arguments);
      cache[arguments[0]] = value;  
      return value;
    }
  }
}

now returns a function so that it can be called multiple times.

Usage

function test(a) {
  console.log('calling test', a);
  return a + 1;
}

const memoized = memoize(test);

memoized(1); // prints calling test and returns 2
memoized(1); // returns 2
memoized(2); // prints calling test and returns 3
Sign up to request clarification or add additional context in comments.

4 Comments

You may also consider changing cache[arguments[0]]!==undefined test to a more sophisticated one like: cache.hasOwnProperty(argument[0]).
I keep getting this error when using online js compilers, chrome console. Can you please help?: Uncaught TypeError: fn.apply is not a function at memoize (<anonymous>:8:20) at <anonymous>:27:13
@user2156888 that error is saying that whatever you are passing into memoize is not a function.
Thanks Anil, looks like I missed return function() { } that you had mentioned earlier. Thanks for the explanation!.
0

I managed to fix my code after suggestions by AnilRedshift. Below is the fixed code.

function memoize(fn) {
  var cache = {};
  return function() {
    var key = JSON.stringify(arguments);
    if (cache[key]) {
      console.log('cache used');
      return cache[key];
    }  
    else {
      var value = fn.apply(this, arguments);
      cache[key] = value;
      console.log('cache not used');
      return value;
    }
  };  
}

var fibonacciMemoized = memoize(function(n) {
    //1,1,2,3,5,8,13,21,34
    if(i<=2)
      return 1;

    var fib = [0,1,1];
    for(var i=3;i<=n;i++) {
      fib[i] = fibonacciMemoized(i-2)+fibonacciMemoized(i-1);
    }

    return fib[n];
});

console.log(fibonacciMemoized(7));

console.log(fibonacciMemoized(9));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.