JavaScript program that return the sum of all odd Fibonacci numbers that are less than or equal to n
Posted by SceDev
Last Updated: March 28, 2024

Write a JavaScript program that return the sum of all odd Fibonacci numbers that are less than or equal to n.

Code:

function sumOfOddFibonacci(num) {
    let prev = 0, curr = 1, result = 0;
    while (curr <= num) {
      if (curr % 2 !== 0) {
        result += curr;
      }
      curr += prev;
      prev = curr - prev;
    }
    return result;
  }

var result = sumOfOddFibonacci(10)

console.log(result);