Even Fibonacci Numbers

Download Video

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.

Solution

/* Better Solution */
let fiboEvenSum = (limit) => {

    let sum = 0

    let a = 1
    let b = 2
    console.log(a)
    console.log(b)
    if(limit >= 2){
        sum = sum + b
    }
    let c = 3
    while(c <= limit){
        console.log(c)
        if(c % 2 === 0){
            sum = sum + c
        }

        a = b
        b = c

        c = a + b
    }

    return sum
}

We have 3 variables - a, b and c. Each time, we generate c by adding a and b together, and then move the variables along by setting a to b and b to c. If the newly generated value (c) is even, then we should add this to our sum. We should stop when c has surpassed our limit.

let fibNumbers = []

/* Generates Fibonacci Numbers Under a Given Limit */
let generateFibNumbers = (limit) => {

  // Base Case
  let a = 0;
  let b = 1;

  fibNumbers.push(a)
  fibNumbers.push(b)

  // Generate Remaing Fib Numbers
  let i;
  for(i = 2; i < limit; i ++){

    let c = a + b
    // Stopping Condition -> when we reach our limit
    if(c > limit){
      return;
    }
    fibNumbers.push(c)

    a = b
    b = c
  }
}

/* Solution Function */
let fiboEvenSum = (limit) => {

  generateFibNumbers(limit)

  // Remove Odd Numbers
  let evenFibNumbers = fibNumbers.filter((number) => {
    return number % 2 === 0
  })

  // Sum Even Fib Numbers
  let sum = 0;
  evenFibNumbers.forEach((number) => {
    sum = sum + number
  })
   
  return sum
}
Ganesh H