Sum All Odd Fibonacci Numbers Memoization

Sum All Odd Fibonacci Numbers Memoization
0

#1

Hi guys, I have found a way of solving this algorithm using recursion, but I can’t submit it to the tests because some of the arguments are too high and it exceeds the maximum call stack.

I understand that recursion isn’t really the best method for this fibonacci exercise, but for the purpose of being able to implement memoization, would someone be able to help me understand it?

I get that I’m trying to get the function to cache it’s answers as it progresses, but I don’t understand how to implement that in my function?

Thanks guys!

Your code so far


function sumFibs(num) {
  
let fib = ((len, arr = [1,1]) => {
	if (arr.length >= len) {
		return arr.filter(el => el % 2 !== 0 && el <= len)
			       .reduce((acc, val) => acc + val, 0);
		}

	arr.push(arr[arr.length - 2] + arr[arr.length - 1]);
	return fib(len, arr);
	

}); 

return fib(num);
}

sumFibs(4);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers/


#2

It is maxing out the stack due to JavaScript limitations and not the tests. This code will not run any the browser or using Node on a server.

I don’t see where you are attempting to implement memoization in your code. Which part do you think you are doing this?