Please Help! Sum of all odd fibonacci numbers!

Please Help! Sum of all odd fibonacci numbers!
0.0 0

#1

The following is my code, and for some reason it keeps crashing. I don’t understand why, can someone please help?

function sumFibs(num) {

var allFibs = [ ];
allFibs[0] = 1;
allFibs[1] = 1;

for (var i = 1; i <= num; i++) {
allFibs.push(allFibs[i] + allFibs[i - 1]);
}

return allFibs;

//This code should return an array with all the fibonacci numbers before the number entered into the function }


#2

Why would you want to return the entire array?

You need to sum all fibonaci numbers.

Add up the entire array together and return that value instead.

Also if you were going to return something in the array it would be only a certain position. Like allFibs[allFibs.length]


#3

I just want to make sure all of the fibonacci numbers are stored in the array, then I plan on filtering out the even numbers and then adding them. But any idea why this code keeps on crashing my browser?


#4

i don’t see your function being called

and then your allFib is only inside the function, the return can’t access it

and you don’t even need the function at this point


#5

This is what I have now. My allFibs is being used in the sumFibs function. When I run the code by calling the function, my browser crashes. Is there something wrong with my loop?

function sumFibs(num) {

var allFibs = [];
allFibs[0] = 1;
allFibs[1] = 1;

for (var i = 1; i <= num; i++) {
allFibs.push(allFibs[i] + allFibs[i - 1]);
}

return allFibs;
}

sumFibs(4);


#6

I really appreciate you taking the time to reply by the way.


#7

maybe instead of
allFibs[0] = 1;
allFibs[1] = 1;
try it with push? i suspect the validator considers those two as undefined since you start with an empty array