Problem 2: Understanding Even Fibonacci Numbers

Tell us what’s happening:
Am i failing to understand the question. It says sum of even valued terms
e.g first 10 sequence is
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
even valued terms here are 2,8,34, -> give a total of 44
Please help maybe am I interpreting things wrong or my brain is tired today…???

Your code so far


function fiboEvenSum(number) {
  // Yes I can do it!//a bit of dynamic programming trickery ...
  // recursion is expensive time efficieny and space-stacks upon stacks..

   let fibs = [1,2]; 

   if(number<=2) {
		return fibs[1]
    };
	
    let j= 2;
    let sum = fibs[1];
    while(j<number) {
    
    let nextFib = fibs[j-1] + fibs[j-2];
    sum = nextFib%2==0? sum+nextFib: sum;
    j++;
    }
    return sum;
}

fiboEvenSum(10);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers

I think what they want is the even numbers in the fibonacci sequence not exceeding 4 million (in the fibonacci, not the even numbers). So
1, 1, 2 , 3, 5, 8, 13, 21, 34, 55, 89 etc etc until you reach 4.000.000.

so you have to return in this case 2, 8 an 34 and not the other numbers until the fibonacci reaches 4 million =).

Thanks for fast response you are a life saver… but Sorry am still lost what must not exceed 4 million? maybe i really am tired from doing express proect whole day…
“so you have to return in this case 2, 8 an 34 and not the other numbers until the fibonacci reaches 4 million =)” i dont get it can you give examples from input = 5,10,23…as in fiboEvenSum(23) whats happening

just look at the test cases in the challenge and you will understand the details of what the challenge wants.

Just checking True or false in the sense

…I looked at the test cases and they tell me my original thinking of adding even numbers was right…problem seemed like i was missing a value in my fibs array first case i added only 2,8,34, …instead 2,8,34, and 144…so i supposed it meant 1,2,3,5,8,13,21,34,55,89,144…etc someone tell me i am still wrong now i pass with my code updated with number+1, to take in extra left out element. I however angry i think the question is unclear or definition of the first n fibs is unclear or i am confused and i am a bit of an obsessive and cant move on without clarity even if i got the answer…

function fiboEvenSum(number) {
// Yes I can do it!//a bit of dynamic programming trickery …
// recursion is expensive time efficieny and space-stacks upon stacks…

let fibs = [1,2];

if(number<=2) {
return fibs[1]
};

let j= 2;
let sum = fibs[1];
while(j<number+1) {

let nextFib = fibs[j-1] + fibs[j-2];
fibs.push(nextFib);
sum = nextFib%2==0? sum+nextFib: sum;
j++;
}
return sum;

}

fiboEvenSum(10);

Yes, you are correct. If you search the forum you will notice this has been asked before though (and answered). Think about your situation as a good exercise in iterative development. You are given some requirements and you write some code then later the requirements change slightly. If your original code has to be rewritten to handle the new requirement (+1) then your original code is probably not so good.

1 Like