Functional Programming - Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

Tell us what’s happening:
Describe your issue in detail here.

Hi, I’m having a tough time filtering out all of the correct numbers in the list (see const squaredIntegers) to then reach the conclusion of this problem.

1: I first tried to create a local variable of numbersToSquareFinal to contain everything I needed to perform on the elements in the array. I went ahead and ran arr.slice() just to preserve/not mutate the original array.

2: After, I ran the first filter() method that seemed to work, somewhat. I wanted to divide each number I passed through and as long as the number could be divided by 1 and return back no remainder, as per the filter(), then that would fit the category of filtering out the numbers with decimals.

3: I ran another filter(), this time passing through each number, and then validating if it was greater than -1 or not, to further filter down that array and refine what numbers are to be squared.

4: Then, I ran a .map() method to square each number.

That’s just me working it out as best I can on my end. I’m thankful that I’ve understood how each of these methods/functions work thus far, but you can see where I’m starting to get somewhat lost. Any tips/helpful pointers greatly appreciated, I’ll keep working on it. I’m just feeling confused and could use the help! (I’m struggling to understand how this isn’t working out.)

Thank you!!

  **Your code so far**
const squareList = arr => {
// Only change code below this line
let numbersToSquareFinal = arr.slice()
.filter(number => number % 1 === 0)
.filter(x => { return x > -1; })
.map(number => number * number)
return arr;
// Only change code above this line

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Functional Programming - Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

Link to the challenge:

what is the output of your function? is it what you want it to be?

if you make this again not a comment, does the array logged matched what you expect the function to return?

Hi, thank you for following up with my question!

I took out the comment and allowed it to console log.

I receive this as my console log:

[ 25, 9 ]
[ -3, 4.8, 5, 3, -3.2 ]

As you can see, something isn’t working with my filter and my map methods.

I was trying to take away all of the negative numbers in the array and also to take away all of the numbers with decimals, that is why I have the filter and map methods running within the constant of squareList.

I’m stuck why this is not working and deleting out the incorrect numbers to perform the math on although it seems that in how I’ve written the filter and map functions, it should work… and it sort of works… but as proven in the console.log of squareList, it logs every single integer in the array.

Any and all help and advice is appreciated!! Thank you!!

The output of your function is defined by the return keyword.

Look at the return statement in your code, what there is written there?

I just (accidentally) solved this on my own, but thank you for helping me brainstorm and providing a helping hand, ilenia!!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.