# Odd fibonacci numbers

**URL:** <https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601>\
**Category:** General\
**Created:** [March 25, 2018, 2:59pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601 "2018-03-25T14:59:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![1ncontr0L](https://avatars.discourse-cdn.com/v4/letter/1/bbe5ce/32.png) [@1ncontr0L](https://forum.freecodecamp.org/u/1ncontr0L)\
**Post date:** [March 25, 2018, 2:59pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/1 "2018-03-25T14:59:14Z")

</div>

Hey coders. something bothers me

Here it is:

```auto
function sumFibs(num) {
  var fib = [1,1];

 Array.prototype.last = function() {
 return this[this.length - 1];
};

 Array.prototype.secondLast = function() {
 return this[this.length - 2]; 
};

while(fib.last() + fib.secondLast() <= num) {
 fib.push(fib.last() + fib.secondLast());
}
return fib.filter(function(elem){
 return elem % 2 !== 0 ; 
}).reduce(function(a, b) {
  return a+b;
});
}

sumFibs(4);

```

This appers to be valid, and the output i get is 5, which does not make sense. Before we filter anything we have an array with numbers [1,1,2,3], and when we use the remainder %2 for each number in the array, we get  
1 % 2 = 1  
1 % 2 = 1  
2 % 2 = 0  
3 % 2 = 1

which equals 3…So where do we get 5?  
another question:  
as you can see in the .filter we added '‘return % 2 !== 0’'  
what is the point of !==0? we reduce the numbers anyway, and its impossible to add 0, so what is the point of that? Please help me, since im a bit confused about this one.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [March 25, 2018, 3:14pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/2 "2018-03-25T15:14:54Z")

</div>

The remainders are not being summed there, that is not what the code does: `elem % 2 !== 0` is saying “the remainder of elem / 2 is not 0” ie it `true` for odd numbers and `false` for even numbers. Filter is being used to filter out the even numbers. Then the odd numbers are added.

Also, all this:

> [@1ncontr0L](#):
>
> ```auto
> Array.prototype.last
> 
> ```

Monkey patching is very, very bad practice, these shouldn’t really be there

---

<div class="post-metadata">

**Author:** ![1ncontr0L](https://avatars.discourse-cdn.com/v4/letter/1/bbe5ce/32.png) [@1ncontr0L](https://forum.freecodecamp.org/u/1ncontr0L)\
**Post date:** [March 25, 2018, 4:20pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/4 "2018-03-25T16:20:42Z")

</div>

forgot to mention, i DELETED !==0 and it worked as well… How?

---

<div class="post-metadata">

**Author:** ![1ncontr0L](https://avatars.discourse-cdn.com/v4/letter/1/bbe5ce/32.png) [@1ncontr0L](https://forum.freecodecamp.org/u/1ncontr0L)\
**Post date:** [March 25, 2018, 8:09pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/5 "2018-03-25T20:09:11Z")

</div>

anyone has a clue why it worked without !==0 included… Then i should get the result of 3 if im not mistaken…

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [March 25, 2018, 8:10pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/6 "2018-03-25T20:10:34Z")

</div>

3 + 1 + 1 (odd Fibonacci numbers \<= 4) is 5, not 3.

---

<div class="post-metadata">

**Author:** ![gebulmer](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gebulmer/32/80907_2.png) [@gebulmer](https://forum.freecodecamp.org/u/gebulmer)\
**Post date:** [March 25, 2018, 8:42pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/7 "2018-03-25T20:42:01Z")

</div>

@1ncontr0L

The `filter` you have _removes_ all even values from the list, giving you `[1,1,3]`.

The following `reduce` takes the sum of the values in the remaining list, aka `1+1+3` as @DanCouper has said.

The reason it still works when you remove the `!== 0` is because of something called _truthiness_

The value of `x%2` is either `0`, or `1`, depending on the value of `x`.  
`if` statements and check if their argument is `truthy` or `falsy`, for these purposes `0` is considered `false` and `1` is considered `true`. `filter` essentially behaves similarly.

I suggest you google some resources for the following topics which will help you understand more about what is happening: “truthiness in javascript”, and “map, reduce, and filter in javascript”.

Best of luck and happy coding!

---

<div class="post-metadata">

**Author:** ![1ncontr0L](https://avatars.discourse-cdn.com/v4/letter/1/bbe5ce/32.png) [@1ncontr0L](https://forum.freecodecamp.org/u/1ncontr0L)\
**Post date:** [March 25, 2018, 8:58pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/8 "2018-03-25T20:58:39Z")

</div>

yes everything is clear to me, until this part with ‘‘elem % 2’’. If i did “elem % 2 !== 0” , then yes, it means it will search for elems whoe result is NOT 0, and it will return them, but since i DELETED "!==’’, leaving only with “elem % 2”, means that now i have (1,1,2,3), and if you calcuate them as this:  
1 % 2 = 1  
1 % 2 = 1  
2 % 2 = 0  
3 % 2 = 1  
it gives me 3…Thats what I’m confused about… I didnt know that its not going to calculate that and it will be leaving me with (1,1,2,3) which .reduce = 5 yes… But i still dont get why it doesnt calculate 😕 guess i have to do a lot of research about that…

---

<div class="post-metadata">

**Author:** ![gebulmer](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gebulmer/32/80907_2.png) [@gebulmer](https://forum.freecodecamp.org/u/gebulmer)\
**Post date:** [March 25, 2018, 9:06pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/9 "2018-03-25T21:06:06Z")

</div>

`filter` uses the “truthiness” of its function argument’s result.

The lines you posted there are correct, i.e `1 % 2 = 1` and `2 % 2 = 0`, but your understand of _how_ they’re _used_ is not.

As `0` is considered `false` and `1` is considered `true`, `filter` in this case will change the list to be just `[1,1,3]`.

The `2` was removed, as the function (`elem % 2`) returned `0` which is considered `false`.

You should definitely read up about “truthiness” and “map, filter, and reduce” to learn more about this. These are really important topics, even if they may be a little hard to understand at first.

---

<div class="post-metadata">

**Author:** ![1ncontr0L](https://avatars.discourse-cdn.com/v4/letter/1/bbe5ce/32.png) [@1ncontr0L](https://forum.freecodecamp.org/u/1ncontr0L)\
**Post date:** [March 25, 2018, 9:14pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/10 "2018-03-25T21:14:31Z")

</div>

okay thanks for explaining, and i have one more question i want to ask

```auto
function findElement(arr, func) {
  
  for (i = 0; i < arr.length; i++) {
    if (func(arr[i])) {
      return arr[i];
    }
  }
  
}
findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });

```

This code only returns 2. My guess is since 2 is truthy, the loop stops searching for others and just gives 2 as a result? But what if i want to return 4 as well with IF statement? I want to return every truthy value

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [March 25, 2018, 9:22pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/11 "2018-03-25T21:22:01Z")

</div>

> [@1ncontr0L](#):
>
> ```auto
> function findElements(arr, func) {
> var matches = []
> for (i = 0; i < arr.length; i++) {
> if (func(arr[i])) {
> matches.push(arr[i]);
> }
> }
> return matches;
> }
> 
> ```

The `return` in your original code means the function returns as soon as it finds the first value that matches. The above pushes every match into an array, and once the loop ends, returns that array.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:23pm UTC](https://forum.freecodecamp.org/t/odd-fibonacci-numbers/181601/12 "2021-01-17T20:23:58Z")

</div>

Closed on January 17, 2021, 8:23pm UTC


