0

I wrote a function to solve Euler #2 in Javascript for adding all the even Fibonacci numbers up to 4,000,000. However, when I run my function, the chrome dev tool keeps giving me zero as the answer. I am not sure why.

function DoEverything() {
  oldnum = 0;
  num = 1;
  total = 0;
  result = addFibNumbers(num, oldnum);
  console.log(result);
} 
function addFibNumbers(num, oldnum) {
  while(num < 4000000) {
    if (num % 2 == 0) {
      newnum = num + oldnum;
      total += newnum;
      oldnum = num;
      num = newnum;
    }
  return total;
  }
}
DoEverything();
2
  • 1
    You may wish to use a smaller numbers when debugging so you can easily check how it's working by logging iterations, i.e. if you logged num at the beginning of your while you could (safely) notice that the while only happens once Commented Apr 28, 2014 at 22:12
  • You can also do some analysis to find that exactly every third number is even. Thus you can do three iteration steps inside the loop and avoid the if statement. Commented May 5, 2014 at 14:00

3 Answers 3

3

The reason its returning 0:

result = addFibNumbers(num, oldnum);//num=1,oldNum=0

//function
while(num < 4000000) { //num is 1, so it enters while
if (num % 2 == 0) {// 1 % 2 == 1, so skip this if
return total;// this ends the function, returning total=0 as nothing was changed

I guess you are looking to do this:

  while(num < 4000000) {
      newnum = num + oldnum;
      if (newnum % 2 == 0 && newnum < 4000000) {
          total += newnum;
      }
      oldnum = num;
      num = newnum;
  }
  return total;
Sign up to request clarification or add additional context in comments.

Comments

0

I would guess it is your while loop

Change this:

while(num < 4000000) {
    if (num % 2 == 0) {
      newnum = num + oldnum;
      total += newnum;
      oldnum = num;
      num = newnum;
    }
  return total;
  }

to this:

while(num < 4000000) {
    if (num % 2 == 0) {
      newnum = num + oldnum;
      total += newnum;
      oldnum = num;
      num = newnum;
    }
}
return total;

Your while loop is useless with a return in it and no if statement to control it's use.

2 Comments

This will make an endless loop
This doesn't appear to work; since num is initialized to 1, it will never go into the if-statement. That means num will never change, and you'll get an infinite loop.
0

In addition to modifying your while statement inside of addFibNumbers() like so:

function addFibNumbers(num, oldnum) {
  while(num < 4000000) {
    newnum = oldnum + num;
    if (oldnum % 2 == 0) {
        total += oldnum;
    }
    oldnum = num;
    num = newnum;
  }
  return total;
}

you will also need to initialize the first two Fibonacci terms to 1 and 2: oldnum = 1; and num = 2;

1 Comment

That is not strictly necessary, even if it is the definition in the Euler project task. The standard Fibonacci sequence starts with 0,1,1,2,3,5,8,..., so changing the start from (0,1) to (1,2) only changes if 0 is added to the sum of even sequence elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.