Write Higher Order Arrow Functions unable to square

Write Higher Order Arrow Functions unable to square
0.0 0

#1

Tell us what’s happening:
I’m able to pass all the test (not like it was hard) but I can’t figure out the method to solve for squared. I know filter takes the argument passed in (x) as in each iteration of a number in an array and sees if the return (=>) is true but i’m having trouble getting the x’s that pass the % 1 test to square

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((x) => (x % 1 === 0) * (x % 1 === 0));
  // 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/66.0.3359.181 Safari/537.36.

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


#2
 const squaredIntegers = arr.filter((x) => (x % 1 === 0) * (x % 1 === 0));

This doesn’t make sense because in the filter callback, you’re doing Boolean * Boolean (true * true, true * false, false * true or false * false). This is not a thing.

You don’t need to do everything in one line. You don’t even need to do it all in one method (you could filter the array in one, then do something to the result of that, for example).


#3

I broke it up into 2 operations: first the filer() and then the map().

scroll below for solution.

const ints = arr.filter( n => n%1 === 0 && n>0 );
const squaredIntegers = ints.map(x => x*x);

squaredIntegers is what gets returned.

Cheers