Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

Tell us what’s happening:
Hey there guys!! can someone tell me why the code registers an infinite loop? so far the test case of (4000000) is the only one that doesn’t check out, and when I log it out, then the infinite loop comes into play…

  **Your code so far**
function sumFibs(num) {
for (var fib = [0, 1], i = 1; i < num; i++) {
  fib.push(fib[i] + fib[i - 1]);
return fib.filter(n => n <= num && n % 2 !== 0).reduce((num, sum) => num + sum);

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

Link to the challenge:

1 Like

I just tested your code and it passes. Remove console.log(fib).

1 Like

Console logging is very slow.

1 Like

thanks guys!! I’ll keep this in mind when working with such high numbers! :joy:

It looks like your for loop logic generates N fibonacci numbers where N=num, when you only need to generate fibonacci numbers up to num in value. So, you’re generating waaay too many numbers. If you fix that, then you shouldn’t need to check n <= num in your filter.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.