Odd fibonacci numbers

Odd fibonacci numbers
0.0 0

#1

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.


#2

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


#3

The filter occurs before the reduce, so the goal of the filter is to create a new array with only odd numbers in it. The return element % 2 !== 0 makes sure only numbers which do not have a remember of 0 are kept. Any number divided by 2 which haw a remainder not equal to 0 is an odd number. Once that filtered array is created, the reduce is just summing up all of those numbers in the new array which the filter created.


#4

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


#5

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


#6

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


#7

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


#8

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…


#9

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.


#10

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


#11

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.