Fabonacci Numbers

I first searched what are fibonacci numbers and how is it counted though and I didn’t read the whole problem which was given that is why I took long to understand and get what was needed from me

Problem

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 10 are 1, 1, 3, and 5.

Approach

I first tried to have an array of all the numbers which I was given an example of then filter odd numbers so I am able to add them. I thought that would work but it never did. I therefor tried to add the number and the parameter. It didn’t work out at all.

final approach

I assigned three variables to 0 and 1, I had a while-loop which says the current number which is one is less or equal to 1 and an if-statement where current number get the remainder of 2 and should not be equal to 0. Inside the if-statement is results which is equal to 0 append current number which is equal to 1.

Outside the if statement I subtracted current number to the previous number which is 0.

Conclusion

It wasn’t difficult and I had to struggle and I had to ask for help though I still don’t get it clearly but hopeful when I redo it again and again I will be able to understand it.