Help with Write Higher Order Arrow Functions

Help with Write Higher Order Arrow Functions
0

#1

Tell us what’s happening:
So I’m having a hard time figuring this arrow function syntax out. If it was a normal for loop I’d do an if statement to check if the number is positive and then if the number is an integer, so I did just that in the arrow function as well. Then in a normal for loop, I’d do integer * integer for every number that passes the check. I have no idea how to integrate that logic into this arrow statement. I checked my current code on pythontutor’s javascript check and it says it should return the correct values, but assigns the pre-squared values anyway.

If anyone could point me in the right direction I’d appreciate it.

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(integer => integer > 0 && integer % 1==0 && integer * integer);
  // 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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


#2

It’s a problem caused by arrow functions specifically: filter takes a function (arrow or otherwise) that has to return true or false: it returns a new array with every element that function returned true for. What you’re currently doing is the same as:

let output = []
for (let i = 0; i < arr.length; i++) {
  let integer = arr[i];
  if (integer > 0 && integer % 1==0 && integer * integer) {
    output.push(integer);
  }
}
return output;

Filter to get the integers, then get the product of the numbers in the resulting array.