Tell us what’s happening:
Hi everybody!
It’s my first post on here so let me know if there are things I’ve missed about posting or if this is in the wrong place to start this thread.
The issue is that one of the offered solutions (Intermediate solution) for Intermediate Algorithm Scripting > Sum All Odd Fibonacci Numbers challenge is not passing all the tests and should probably be adjusted. Unfortunately I can’t figure out where the problem might lay exactly.
The code below is the offered provided found under ‘Get a Hint’ button.
Your code so far
function sumFibs(num) {
// Perform checks for the validity of the input
if (num < 0) return -1;
if (num === 0 || num === 1) return 1;
// Create an array of fib numbers till num
const arrFib = [1, 1];
let nextFib = 0;
// We put the new Fibonacci numbers to the front so we
// don't need to calculate the length of the array on each
// iteration
while((nextFib = arrFib[0] + arrFib[1]) <= num) {
arrFib.unshift(nextFib);
}
// Sum only the odd numbers and return the value
return arrFib.reduce((acc, curr) => {
return acc + curr * (curr % 2);
});
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) 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