Skip to content
Advertisement

Unsure about syntax/methods. I believe my code should work but it does not

Prompt: Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num. The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8. For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5.

The code I wrote for this is:

JavaScript

but i get 23 when it should be 10, I’m not sure why this doesn’t work because i feel this would work in java. I have also tried to do arr[i] == queef but that also does not work. I am missing something or should this work?

Advertisement

Answer

I think your error relies in

JavaScript

I believe you’re generating an amount of numbers till num instead of the value itself. Try with something like this (I tried to keep your style):

JavaScript
Advertisement