Odd fibonacci numbers

Hey coders. something bothers me

Here it is:

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.

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:

Monkey patching is very, very bad practice, these shouldnā€™t really be there

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

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

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

@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!

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 :confused: guess i have to do a lot of research about thatā€¦

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.

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

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

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.

1 Like