Using regex in the Use Higher-Order Functions map, etc to Solve Complex problems

To start off with, this code passes the test, so I’m covering it with spoiler tags.

For some reason I didn’t think of using parseInt or Number.isInteger for solving this exercise and I was struggling to think of a way to filter out decimals, then I thought of using the regex .test() method looking for the period. Aside from not being as efficient, what would be the most likely pitfalls of this approach?

Thank you!

  **Your code so far**

const squareList = arr => {
let regex = /\./;
arr = arr
  .filter(x => x > 0 && regex.test(x) === false)
  .map(x => x * x);
return arr;
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
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/95.0.4638.69 Safari/537.36

Challenge: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

Link to the challenge:

Honestly, the efficiency and the fact that its not a typical way to check for integers is the only thing I would be worried about.

1 Like

Awesome, thank you for answering! :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.