Comparing solutions: Write Higher Order Arrow Functions

Tell us what’s happening:
I’m just wanna know what’s the difference between my solution (pretty racoon solution) and the spoiled solution:

Your code so far

//My solution
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  let first =  arr.filter (arr => arr % 1 == 0 && arr > 0)
  const squaredIntegers = first.map (arr => arr * arr);
  //arr.map (arr => arr * arr); 
  console.log (squaredIntegers)
    // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
//Freecodecamp "basic" solution
const squareList = (arr) => {
      "use strict";
      const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );
      return squaredIntegers;
    };

    // test your code
    const squaredIntegers = squareList(realNumberArray);
    console.log(squaredIntegers);

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

Yours has a non-bizarre, sensible filter callback. And imo num * num (or even num ** 2) is more readable than Math.pow, but ymmv

Otherwise, you can chain the functions:

return originalArray.filter(fun).map(fun);

You don’t need intermediate variable assignment.

Also, if you do want to assign to split things up, then use const for both of them. You aren’t reassigning a variable, and that’s the only reason to use let.

1 Like

Nice. Well explained!