Sum of odd fibonacci numbers question

Hello,
I am working on this challenge: Intermediate Algorithm Scripting: Sum All Odd Fibonacci Numbers

I’m trying to test if my fibonacci solution is correct before adding up all the prime numbers in it. However, while testing my fibonacci, my solution seems to be right except for the part where it’s supposed to stop if the last number is less than or equal to num. Is there anything I am not seeing here?


function sumFibs(num) {
  
  let fibonacci = [1,1]
  for(var i = 0; i <= num; i++){
    fibonacci.push(fibonacci[i] + fibonacci[fibonacci.length - 1]);
  }
  return fibonacci;
}

console.log(sumFibs(4));

This returns => (7) [1, 1, 2, 3, 5, 8, 13]

Would appreciate some enlightenment :slight_smile:

Your loop says, roughly translated from code to English, "add the next num+1 Fibbonacci numbers to the fibonacci array. I suspect you want instead to stop adding numbers when the number added is bigger than num.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

I see it now, thank you!!

1 Like