Skip to content

Commit

Permalink
Create 2-even_fibonacci_numbers.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshh123 committed Oct 11, 2020
1 parent e7b77b4 commit d06616e
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions 2-even_fibonacci_numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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
}

console.log(fiboEvenSum(1000))

0 comments on commit d06616e

Please sign in to comment.