scrimba
Introduction to Project Euler
Even Fibonacci Numbers
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

AboutCommentsNotes
Even Fibonacci Numbers
Expand for more info
index.js
run
preview
console
// 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 temp, a = 0, b = 1, sum = 0;
while (n > 0) {
temp = a;
a = b;
b += temp;
n--;
// console.log(b);
if (b % 2 === 0) {
sum += b;
}
}

console.log(sum);
return sum;
}

fiboEvenSum(10); // Correct: 44, Expects: 188
// fiboEvenSum(23); // Correct: 60696, Expects: 60696
// fiboEvenSum(43); // Correct: 350704366, Expects: 1485607536
Console
›
44
,
index.html
-4:44