Write Higher Order Arrow Functions#

Tell us what’s happening:
Hey guys !! I’m working my way to be the “oh no, him again !!” guy :slight_smile:.
So here is my code for this chalenge, the arrow syntax seems correct, however the output is wrong.

If I may suggest : prolly it’s better to include challenges that explain the reduce, map and filter methods before this one, cause I was completely lost and had no idea that they were array methods, had to think for a while and do some research to understand what they are and we use them, and still I don’t get it right, thank you.

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
  const squaredIntegers = arr;
  squaredIntegers.filter(integ => integ > 0 && Number.isInteger(integ));
  squaredIntegers.map(square => Math.sqrt(square));
  
  // 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/76.0.3809.132 Safari/537.36.

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

.filter() and .map() both return new arrays. They do not modify the array that they are called on.

Well I can’t use them to modify the current array then as it is a constant, is there other methods that can do the job, or I am supposed to translate a conditional loop function to arrow syntax to get it done, I’m a lil lost, thank you.

I just had a look at that lesson and they want you to write a higher order function to get the value for squaredIntegers.
You can chain the functions; .filter().map()

This is the order of events:

  • filter first to reduce the number of array items you have to run a computation against
  • this creates a new array that is passed to the map() function via the chaining
  • then you run the ‘math(squaring not square root)’ against the elements in the new array

squaredIntegers.filter... and squaredIntegers.map return new arrays but that array is not being assigned to anything or used in any way.

How can I chain the functions ? I can’t figure out the syntax, I tried to pass the filter() as argument in the map(), but it didn’t work.

arr is the array passed into the function
you want to run .filter() and .map() against that array and output the value to squaredIntegers.

so you would start with:
const squaredIntegers = arr.filter(do stuff here to filter array).map(do stuff here to square the array elements)

1 Like

Chaining works like this:

stuff = thing.someMethod().someOtherMethod().thirdChainedMethod();

This is the same as

stuff = thing.someMethod();
stuff = stuff.someOtherMethod();
stuff = stuff.thirdChainedMethod();

exactly… like @ArielLeslie explains.

Chaining is passing the result of one function on to the next function

@ArielLeslie it worked thank you.
@Sonnerz I saw your post late, instead of doing what you said, I deleted the return array, and put return on the expression, it worked, but I was supposed to do as you said.