Write Higher Order Arrow Functions,

Write Higher Order Arrow Functions,
0.0 0

#1

Tell us what’s happening:
This code should work, it is working but it is not giving square of int, I can’t figureout why

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
  const squaredIntegers = arr.filter((int) => {
    if(Number.isInteger(int)){
      return Math.pow(int,2);
          }
  })
  // 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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

What am I doing wrong, my code is not giving back square


#2

Filter doesn’t work like that: what it does is take an array, and the callback function has to return true or false. Only the values for which it returns true are kept.

So this will work:

arr.filter(value => Number.isInteger(value))

So you go through the array, and any integer values are going to be true , so they get kept.

The second part, the Math.pow, you would want to use map, which takes an array, and runs a function transforms every element. filter for integers first, then once you have that array of just integers, map over it and apply Math.pow.