Higher-Order Arrow Functions

Hi.

I’m finding this challenge to be a handful.

Here’s my attempt to crack it:

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.filter((realNumberArray) => realNumberArray > 0 &&);
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

I’d be grateful for any help.

const squaredIntegers.filter((realNumberArray) => realNumberArray > 0 &&);

This is not a valid syntax, i think something has been messed up ^^

That said, consider to break down the problem: you are asked to return an array of the square values only of positive integer values. You could follow these steps:

  • Filter out only the valid values, which means filter out anything that does not respect the specs ( positive AND integer)

  • Map over the values of that new array and return the square of those values.

Since filter returns a new array you can concat the two functions: something like
const newArr = originalArr.filter(...).map(...)

Good luck!

1 Like

Thanks for your help but I’m at a loss on this one. We’re not told what the map(), filter() and reduce() methods do.

const squaredIntegers = realNumberArray.filter().map();

Consider those methods as functions that iterates upon each element of the array they are called against.
Something like

oldArray.map(x => console.log(x))

In the above example every item in the old array will be logged into the console.

const newArray= oldArray.filter(x => x < 20)

Here every item in the old array will be checked against the expression x<20: if it returns true the item will be inserted into the new array, if it return false the item will be discarded;

Now, if you would log only the items < 20, you could operate as follows:

oldArray.filter(x => x < 20).map( y => console.log(y));

I hope this clarify a bit ^^

Happy coding! :slight_smile:

1 Like

Thank you for your help. I’ve just solved this challenge. I had a look at MDN (Mozilla Developer Network) and found a method called Number.isInteger() which tests for whole numbers in the array.

Thank you.

1 Like