I need help with the "Write Higher Order Arrow Functions" challenge

In this challenge i have to Use arrow function syntax to compute the square of only the positive integers (decimal numbers are not integers) in the array realNumberArray and store the new array in the variable squaredIntegers .

the code below is what i’ve come up with and it’s not enough to pass this challenge, i need someone to orient me in the right direction so that i can solve this challenge.

Your code so far


const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  realNumberArray.filter((arr) => arr !== null && parseInt())
  const squaredIntegers = arr;
  // 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/74.0.3729.169 Safari/537.36.

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

The result of this doesn’t get stored to any variable, so it’s discarded.

The parameter passed to the callback for filter isn’t the whole array, but instead each element of the array.

For the purposes of this challenge, you can assume none of the elements are null. All are real numbers, but not all are positive integers.

This function is used for parsing strings into integers. None of the array elements are strings. You’re also not passing anything to this function, so it’s as if you’re calling it on undefined.

You’re just returning the original array unchanged.


What you need to do (simplest way) is:

  • filter out the non-positive numbers and non-integers. Store the result to a new variable.
  • map the resulting array to a new array containing the squares of each member. Store this to a new variable.
  • Return that final array.
2 Likes

this is the code now , thanks.

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  arr = arr.filter((elem) => Number.isInteger(elem) && elem >= 0 )
  const squaredIntegers = arr.map((elem) => elem * elem);
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
1 Like

Nice implementation! It seems like the pseudocode really helped, so for future challenges, you might find it beneficial to follow the same approach (write pseudocode, then implement it).


Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

sure, i’ll do it next time