I have a question about implementing the solution for Fibonacci Number.
.
Below is my code
=======================================================================================
/**
* @param {number} N
* @return {number}
*/
var fib = function(N) {
var hash={};
function fibon(number)
{
if(hash.hasOwnProperty(number))
{
return hash[number];
}
var result;
if(number < 2 && number >=0)
{
result=number;
}
else
{
result=fibon(number-1)+fibon(number-2);
}
hash[number]=result;
return result;
}
fibon(N);
};=======================================================================================
I try to use hash to act as a cache to avoid duplicate calculation.
However, when I run the code, it return undefined.
I have checked the variable hash and it seems fine.
May I know is there any problem in my code? Thank you for your help.