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
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ā.
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ā¦
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.