Sum All Odd Fibonacci Numbers- Help please

Tell us what’s happening:
Why is it not passing? What mistakes am I making? thank you. All help appreciated.

Your code so far


function sumfibs(num){
  let arr= [1,1];
  function isOdd(value) {
  return value%2!==0 ;
}
  function add(a, b) {
    return a + b;
}
  while (arr[arr.length-1]+arr[arr.length-2]<num){
    let newNum= arr[arr.length-1]+arr[arr.length-2];
    arr.push(newNum);
  }
  //console.log(arr);
  var filtered = arr.filter(isOdd);
  var sumOdd = filtered.reduce(add);
  console.log(sumOdd);
}
sumfibs(1000);

Your browser information:

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

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

You have a couple major issues.

  1. You have named your function something different than what the original function was named.

  2. You are not returning a value from your function.

  3. Your function does not meet the expectations of the instructions requirement (see bold part below).

Given a positive integer num , return the sum of all odd Fibonacci numbers that are less than or equal to num

function sumFibs(num){
  let arr= [1,1];
  function isOdd(value) {
  return value%2!==0 ;
}
  function add(a, b) {
    return a + b;
}
  // while the sum of the last two values in the array are less than the num argument passed, we push the sum of the last two numbers to the holder array.
  while (arr[arr.length-1]+arr[arr.length-2]<=num){
    let newNum= arr[arr.length-1]+arr[arr.length-2];
    arr.push(newNum);
  }
  var filtered = arr.filter(isOdd);
  var sumOdd = filtered.reduce(add);
   return sumOdd;
}
sumFibs(75025);

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

1 Like

Here’s my code. Hope it helps

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

1 Like