sum of the odd terms of the fibonacci sequence code example

Example 1: sum all odd fibonacci numbers

function sumFibs(num) {
  var prevNumber = 0;
  var currNumber = 1;
  var result = 0;
  while (currNumber <= num) {
    if (currNumber % 2 !== 0) {
      result += currNumber;
    }

    currNumber += prevNumber;
    prevNumber = currNumber - prevNumber;
  }

  return result;
}

// test here
sumFibs(4);

Example 2: sum all odd fibonacci numbers

const sumFibs = num => {
  // Fibonacci always starts with a [1,1] so, we just initialize it
  const fibNum = [1,1] // Get all the fibonacci numbers for that range
  const fibOdd = [1,1] // Get all the odd fibonacci numbers for that range
  
  // Loop for through for n times(num)
  for(let i = 0; i < num; i++){
    // Add the current index to the next index to get the later fib number
    const fibAdd = fibNum[i] + fibNum[fibNum.length - 1]

    if(fibAdd > num) break; // Stop pushing or looping if added indexes is more than number(num)
    fibNum.push(fibAdd) // Push to all fibNum if added indexes is less than the number(num)

    // Push odd fib numbers to fibOdd, so we can now uniquely add only odd numbers
    if(fibAdd % 2 !== 0) fibOdd.push(fibAdd)
  }

  // Return reduced or added fibonacci odd numberss
  return fibOdd.reduce((acc,cur) => acc + cur)
}

sumFibs(75024);

// With love @kouqhar