Project Euler: Problem 2: Even Fibonacci Numbers failed to recognize the "return value;"

Tell us what’s happening:

Your code so far

The default function template as follows:

function fiboEvenSum(n) {
  return true;
}

As long as I added some code to it like this:

function fiboEvenSum(n) {
  let fib_sum = 0
  let sequence = [1, 2]

  let i = 2
  while (sequence.at(-1) <= n)
  {
      let next_seq = sequence[i - 1] + sequence[i - 2]
  }

  return fib_sum;
}

When I run the "Check Your Code" button, it immediately gave me a CROSS for "fiboEvenSum(10) should return a number."

So no matter what additional code I added, it couldn't pass the test as the "Check Your Code" ignore the "return fib_sum;" statement at the end.


I am not sure I understand what you are reporting, can you add more details?

The problem is when I added the while statement, the “Check Your Code” failed to recognize the last line of “return fib_sum;” statement.

Anyway, I resolved the issue after using a for loop as follows:

function fiboEvenSum(n) {
  let sequence = [1, 2]
  let fib_sum = 2
  let next_sequence

  for (let i = 2; i <= n; ++i) {
    next_sequence = sequence[i - 1] + sequence[i - 2]
    if (next_sequence <= n) {
      sequence.push(next_sequence)
      if (next_sequence % 2 == 0)
        fib_sum += next_sequence
    }
    else
      break
  }
  
  return fib_sum;
}

sequence never changes, so this loop never ends. The tests were not ignoring your return, but it was never being executed