# Project Euler: Problem 2: Even Fibonacci Numbers

hello every body I am in the problem N.2 of Project Euler challenges and that’s my solution
`
function fiboEvenSum(number) {
// You can do it!
let prev = 1 ;
let next = 2 ;
let sum = 2 ;
for ( let i = 0 ; i<number ; i++) {

``````    [prev,next] = [next,prev+next] ;
if(next%2===0) sum+=next ;
}
return sum;
``````

}`

and I got all tests correct except that test “Your function is not returning the correct result using our tests values.”
and I don’t know why I get this wrong test ?
any ideas ? may be its a bug .

It’s definitely not a bug because the condition/logic in your `for` loop is incorrect—you should be stopping the loop once `next` is greater than `number` but your for loop just keeps increasing. If you log `next` to the console after you have calculated `next` you will see the problem:

``````for (let i = 0 ; i < number; i++) {

[prev, next] = [next, prev+next];
console.log(next); // For example, if `number` = 3, then next should never exceed 3
}
``````

Good luck!

the probme is missleading. and Project Euler 2 has a bug in description.
first 10 terms should be 1,1,2,3,5,8 and so on … not 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

therefore the even numbers to add are 2 , 8 , 34 and 144… if function is provided with argument 10.

but on freecodecamp is wrong .

MostafaSalama
To pass your solution change just this

let prev = 1 ;
let next = 1 ;
let sum = 0 ;

I can confirm your solution is good with this changes.
as you done everything perfect you just been mislead
by the wrong problem description.

honmanyau you are wrong.
My solution to pass was

function fiboEvenSum(number) {
let first = 1,second = 1, tarray= [],a= 0;

``````        while(tarray.length < number){
a = first + second;
first =second;
second = a;

tarray.push(a);
}
var farray =    tarray.filter((el)=>{
if(el % 2 == 0)
return el;
});

var total = farray.reduce((a,b)=>{

return a + b;

});
``````

console.log(farray,total);
}

fiboEvenSum(10);

1 Like

It’s not misleading. “Even valued” has a vastly different meaning to “even numbered” or “even terms”. Many have solved this problem without any issues, you can see the statistics here.

Please wrap your solution with the `[spoiler][/spoiler]` tags, too!

Read again what I posted and test it. Read again and try to understand.
It is wrong description.
Fib numbers are not 1,2,3,5,8
but 1,1,2,3,5,8 … huge difference when applied the logic.

Mostafa`s solution is good with variables changed as I told you in post above.

function fiboEvenSum(number) {
// You can do it!
let prev = 1 ;
let next = 2 ; !!! here should be 1 not 2
let sum = 2 ; !!! here should be 0 not 2
for ( let i = 0 ; i<number ; i++) {

``````[prev,next] = [next,prev+next] ;
if(next%2===0) sum+=next ;
``````

}
return sum;
}

You are absolutely right, I jumped to the conclusion after seeing that the description is exactly the same as the one on Project Euler and glossed over the rest of the detail I was clearly doing anything but reading. Many apologies.

I haven’t actually done the problem on freeCodeCamp until just now and it appears that the implementation on freeCodeCamp is indeed quite “wrong” for quite a few reasons.

Not to argue with you because I was definitely wrong to have made assumptions about the implementation on freeCodeCamp without having attempted it—but now that I’ve done the freeCodeCamp one as well, I just want to clarify the point that you made about the difference: it doesn’t look like the issue is because of the missing 1 in the Fibonacci series. Your code is definitely right, but using your logic, `144` is the 12th term. If we start from 1, 2, 3, 5, 8… etc. then `144` is the 11th term. (By the way, even without the additional 1 at the beginning these are still Fibonacci numbers, by the way, just because it’s missing a 1 doesn’t mean that they are not part of the series).

So the issue is because whomever implemented the test logic is actually counting from `0` and included `10` as well, which, again, is really weird because that’s counting 11 terms.

In any case, and once again, many apologies for the assumptions I’ve made simply because the description is the same as the one I’ve attempted on projecteuler.net. Thank you for taking the time to explain the issue and correct me, too!

P.S. Do you want to open an issue on GitHub about this? If not, I’m more than happy to do so.

2 Likes

This was totally my problem as well, thanks! Your code is much simpler than mine and probably takes up way less memory too haha! This is what I came up with which has two versions and some notes about other things that seem wrong in freeCodeCamp’s question…

``````//The exercise specifically says the Fibonacci sequence whose values do NOT exceed four million...which would be this:

function fiboEvenSum(x) {
var list = [1, 1];
for (var i = 1; i <= x; i++) {
let a = list[i] + list[i - 1];
if (a < 4000000) {
list.push(a);
} else {
break;
}
}
console.log(list);
return list.filter(i => i % 2 == 0).reduce((a, b) => a + b);
}

console.log(fiboEvenSum(43));

//...but one of the tests says fibEvenSum(43) should return 1485607536 which is quite a bit higher than the 4000000 term which we cross between the 30 and 31st terms...

//   ¯\_(ツ)_/¯    here ya go...

function fiboEvenSum(x) {
var list = [1, 1];
for (var i = 1; i <= x; i++) {
let a = list[i] + list[i - 1];
list.push(a);
}
console.log(list);
return list.filter(i => i % 2 == 0).reduce((a, b) => a + b);
}

console.log(fiboEvenSum(43));

``````

I’m leaving this message here for the poor souls who have trouble deciphering the description of the problem (myself included). I spent a half an hour tweaking my code because I thought that it means the sum of all numbers (it does not).

The problem can be rephrased like this:

`By considering the terms in the Fibonacci sequence that are smaller or equal to 4 million (4 000 000)`.

There, you just avoided wasting half an hour.

Welcome, florian.

If you were going about it in any other way than:

By considering the terms in the Fibonacci sequence whose values do not exceed `n` , find the sum of the even-valued terms

Then, you would have an infinite loop, because there are an infinite number of Fibonacci numbers. The fCC tests only go up to *4 000 000, but your code should still be able to find the sum of numbers higher, if written correctly.

If you are having any issue with completing the challenges, open a new topic (unless you find an existing one). The fCC community are glad to help.

Honestly that’s not a big improvement over the original definition because you are using “values” and “terms” to talk about the same thing, that’s part of how I was misguided in the first place. Also, when I read values I think about the overall sum of the sequence, so that’s not good either. The word “term” doesn’t bring confusion with it, they should’ve only used that.