Sum All Odd Fibonacci Numbers sumFibs(75025) fails

Dear Coding Community!

My code passes all the tests BUT sumFibs(75025).
The funny thing is:
sumFibs(75024) returns 60696
sumFibs(75025) returns 60696 too;
sumFibs(75026) returns 135721 (that’s what sum(75025) should return.

I somehow guess it is an “off by 1” error, but declaring the function with higher and lower numbers than “75025” works fine (according to the checklist…)

Anyway, I’m grateful for any piece of advice!

``````
function sumFibs(num) {
{
if (num == 1) { return 1; }

let x = 1, y = 0;

let arr = [1]

for (let i = 0; i < num; i++) {
let result = x + y;

y = x;
x = result
arr.push(result)
}

console.log(arr)
let newArr = arr.filter(el => (el % 2 == 1) && el < num);
console.log(newArr)
var sumArr = newArr.reduce((el1, el2) => el1 + el2);
}

return sumArr;
}

sumFibs(4);
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.44`

Challenge: Sum All Odd Fibonacci Numbers

It probably fails for the same reason that `console.log(sumFibs(5))` gives the wrong answer.

2 Likes

I found the mistake thanks to your hint (equal or less, not merely less…)

``````let newArr = arr.filter(el => (el % 2 == 1) && el <= num);
``````
1 Like

A few notes:

1. You shouldn’t use `var` - its a legacy feature of Javascript

2. I’d try to make a version of this where you don’t make an array - in general, you should only use an array (or other storage) for something you need more than once, but you only use the data in the array once

3. I’d use `const` for array declarations unless you plan on replacing the entire array

1 Like

3. That was a quick fix.
2. Will take longer - I’ll try to get back to this later

1. I had to make a workaround to make the value of sumArr available outside its scope. Using “let” wouldn’t have allowed me to do that in my initial code. I added an “else”-statement:
``````function sumFibs(num) {
{
if (num == 1) { return 1; }
else {
let x = 1, y = 0;

const arr = [1]

for (let i = 0; i < num; i++) {
let result = x + y;

y = x;
x = result
arr.push(result)
}

const newArr = arr.filter(el => (el % 2 == 1) && (el <= num));

const sumArr = newArr.reduce((el1, el2) => el1 + el2);
return sumArr;
}

}
}

sumFibs(5);
``````

I’d simplify your scopes. It is strange in Javascript (but more common in other languages like C) to use {} without a control statement to establish a limited scope. The only reason to do this is to intentionally limit the scope of a variable, but all of your variables are already limited in scope to your function body.

@JeremyLT
I was able to eliminate one pair of curly brackets and the else-statement (including its curly bracket), that makes (2*{} and “else”) removed from the code. Did you spot any more redunancies scope-wise?

``````function sumFibs(num) {

if (num == 1) { return 1; }

let x = 1, y = 0;

const arr = [1]

for (let i = 0; i < num; i++) {
let result = x + y;

y = x;
x = result
arr.push(result)
}

const newArr = arr.filter(el => (el % 2 == 1) && (el <= num));

const sumArr = newArr.reduce((el1, el2) => el1 + el2);
return sumArr;

}

sumFibs(5);
``````

Yeah, that’s more what someone would expect from Javascript.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.