Sum All Odd Fibonacci Numbers, solved?

I solved the task, but I didn’t. For some reason, it doesn’t pass the last given argument -seen in the code snippet.

function sumFibs(num) {
let arr = [1]
let summer = [ ]
for (let sum = 1; sum < num;){
summer = []
arr.push(sum)
summer = arr.slice(-2)
 sum = summer.reduce((a, b) => a + b)
}
let filt = arr.filter((x) => x % 2 !== 0)
return filt.reduce((a, b) => a + b)
 }
sumFibs(75025)

I know this method is inefficient, but regardless, shouldn’t it work tho?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

If it doesn’t work for all of the tests, then it doesn’t work.

I have a really hard time following what you are trying to do, here. First of all, this:

  for (let sum = 1; sum < num;){

is not a semantically correct way to use a for loop. One of the other loops would make more sense.

Why do you keep initializing summer. It’s never used without assigning it a value.

I mean, I guess I see what you’re trying to do, it’s just difficult to read. In any case, the problem is in your loop logic. I was able to make a small change and it worked. Notice the diffence in the last two cases, they’re trying to test an “edge case” there.

1 Like

And summer always has exactly 2 numbers - Why are you using reduce?

And please always provide a link to the challenge: