-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7b77b4
commit d06616e
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |