How's my code? Is there something I can improve? Fibonacci numbers Algorithm

How's my code? Is there something I can improve? Fibonacci numbers Algorithm
0.0 0

#1

I read about recursion and tried to apply it to my algorithm and succeed :smiley:
I first made a function which makes fibonacci stuff and then put it in another method - the main method required by FCC/
Here’s my code:

function sumFibs(num) {
// Function to make fibonacci and return the sum of odd numbers
// that meet the requirements
function fib(startX, startY, oddSum,num) {
// check if the nuber is odd and meet the requirements to
// add to the sum
if (startX % 2 !== 0 && startX <= num) {
oddSum += startX;
}

	var sum = startX + startY;
	// temp for storing startY
	var buff = startY;
	startY = sum;
	startX = buff;
			

	// make the fibonacci loop and don't exceed memory
	if (startX + startY < num * num) {
		return(fib(startX,startY,oddSum,num));
	}
	return oddSum;
	
	// body...

}
// the starting numbers are 1 and 1 instead of 0 for convenient purposes
return fib(1,1,0,num);

}
return sumFibs(4);

`

`

Is there somthing I can improve?


#2

If u here change startX to startY u would reduce looping by one

Why u compare against num * num here:

if (startX + startY < num * num) {

Also if u want u can do this without temp variable by:

startY = startX + startY;
startX = startY - startX;

#3

Thank you, very helpful!