-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheuler-problem-2.js
53 lines (39 loc) · 1.25 KB
/
euler-problem-2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Project Euler - Problem 2
// Even Fibonacci
// Starting with the first two terms of the Fibonacci sequence being 1 and 2, find the sum of the even-valued terms for the terms in the Fibonacci sequence whose values do not exceed four million.
// create a function that will find the sum of the even-valued terms for the terms in the Fibonacci sequence whose values do not exceed a passed in upper limit
function evenValuedFibonacciSum(limit) {
var sum = 0;
var term1 = 1;
var term2 = 2;
var temp;
// nested recursive helper function
function sumNextTerms() {
// check if both terms exceed the limit, if so then return
if (term2 > limit) {
return;
} else {
// check that term2 does not exceed limit
if (term2 <= limit) {
// check if term2 is even
if (term2 % 2 === 0) {
// add to sum
sum += term2;
}
}
// update the Fibonacci terms
temp = term2;
term2 = term1 + term2;
term1 = temp;
// make recursive function call
sumNextTerms();
}
}
// start recursive calls
sumNextTerms();
return sum;
}
// test
console.log(evenValuedFibonacciSum(10));
console.log(evenValuedFibonacciSum(100));
console.log(evenValuedFibonacciSum(4000000));