Write Higher Order Arrow Functions1

just figured out what was wrong. If anyone else gets stuck, check out this vid at 20 : 37. This helped me loads and make this concept clear https://www.youtube.com/watch?v=rRgD1yVwIvE.

Hope that helps somebody

3 Likes

It’s a great idea! and i did that, however my filter was quite different, instead of using Modulus (Division Remainder) x = x % y i used the value > 0 and Number.isInteger(value) when the number is a integer.

I was confused with the input of filter() and map(), hopefully this can help someone:

arr.filter(condition) takes each element from the arr array, and match it with the condition in its argument, returning only those that returned true for the condition comparisons.
so you may be thinking of doing something like this at first:

arr.filter( each array element > 0);

however, we don’t have a variable name to manipulate each array element to do the larger than zero comparison, so we can make a function that passes each array element given by filter() into a variable named num, and return the ones that matches true to the conditions, and filter() will match and only keep those numbers where the function returned true:

arr.filter(
function (num) {
return num > 0;
});

with arrow function, it will become:
arr.filter( (num) => num > 0 );

add in the check for integer, which can be done by:
arr.filter( (num) => num > 0 && Number.isInteger(num) );

now we have an array left with only integer larger than zero, and we want to square each one using Math.pow(base, power), since we don’t want to loop through each element, we can use map(argument), which apply its argument to each array element:
array elements after filtering.map( squaring );

The same problem with map(), we have no variable name to perform the squaring, so we make a function again:
array elements after filtering.map( (num) => Math.pow(num, 2) );

and since the filter() method returns all the array elements after filtering, we can chain them together:
arr.filter( (num) => num > 0 && Number.isInteger(token) ) .map ( (num) => Math.pow(num, 2) );

finally, you can assign this whole thing to the variable you want:
const squaredIntegers = arr.filter((token) =>
Number.isInteger(token) && token > 0).map((token) => Math.pow(token,2));

If I have a misconception, please message me, I really would like to know. Thanks!

2 Likes

Thank you very much for your excellent description.

@conicbe, Thank you ver much for this information. Very helpful. This video lecture made all the higher order functions – forEach, filter, map, sort, reduce – easy to grasp and understand.

Could you elaborate on how this could be achieved with reduce(). I just looked into that function and it seems that it would return a single number.
I would love to learn the finer points of this function.

I almost did it right, I used the filter function, but still get an error because it is not squared.
here is my one line code:
const squaredIntegers = realNumberArray.filter((arr)=>parseInt(arr) > 0);

the error I get is:
squaredIntegers should be [16, 1764, 36]

Can anyone help me please?

you need to also change the values in the array. You can chain different methods one after the other, for example you can chain a map() that would square the items

note that filter doesn’t change the items in the array, so you need to consider also how to remove the items that are not whole numbers

You are not limited to returning numbers, reduce can return anything, it depends on the starting value you set.

for example this will return the halved numbers of only the positive ones:

function halfPositive (array) {
   return array.reduce((acc, cur) => {
   if (cur > 0) {
      acc = [...acc, cur/2]
   }
   return acc;
}, [])
}
const arr = [-1, 1, 2, -5, 4.55, -4.55]
console.log(halfPositive(arr));   // [0.5,1,2.275]