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.