Write Higher Order Arrow Functions(i can understand filter function has to be used here to solve it, but how ? can any one give me solution for it or nay alternative example)

Tell us what’s happening:

Your code so far


const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
  "use strict";
  // change code below this line
   for(let x=0;x<arr.length;x++){
    realNumberArray[x];
    if(realNumberArray[x]>=1 && realNumberArray[x]%1===0){
       realNumberArray=[realNumberArray[x]*realNumberArray];
    }
  }
  const squaredIntegers = arr;
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions/

When using higher order functions, there’s no need to write your own loops, because filter(), map(), reduce() and others do this thing for you. So wipe out the whole code you’ve written now and take a close look at the example on the challenge page:
FBPosts.filter((post) => post.thumbnail !== null && post.shares > 100 && post.likes > 500)
What this does is it takes a FBPosts array itself and calls a filter function on it. That’s it, it automatically gets its’ every value evaluated. Then you apply the filter function which is the body in the parentheses, in this case (post) => post.thumbnail !== null && post.shares > 100 && post.likes > 500).
It reads return a post which is not null, has more than 100 shares and more than 500 likes. That’s it. So now what you need to do is take realNumberArray (which is given as arr parameter), call filter() on it and set the rules in the function body needed to pass this challenge. It has to assigned to squaredIntegers constant. It’s value is going to be a function call which returns you the desired value - a filtered array. Then you need to go further and square those numbers in that array, that filter() returned. Only then return squaredIntegers. Hope this helps. If not - let me know :wink:

1 Like

Thanks for clarifying the purpose of higher order functions. :smile: