FCC Project Euler: Problem 2: Even Fibonacci Numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed nth term, find the sum of the even-valued terms.

function fiboEvenSum(n) {
let total = 0;
let fibArr = [1,2];

if (n < 2) return fibArr[1];

for (let i = 2; i <= n; i++) {
fibArr.push(fibArr[i-2] + fibArr[i-1]);
}

for (let i = 0; i < fibArr.length; i++) {
if (fibArr[i] % 2 === 0) {
total += fibArr[i];
}
}
return total;
}

fiboEvenSum(10);

Comments

Popular posts from this blog

Code Wars: longest_palindrome (6 kyu)

Code Wars: Data Reverse (6 kyu)

Code Wars: Replace With Alphabet Position