Write Higher Order Arrow Functions | .reduce vs .map

Write Higher Order Arrow Functions | .reduce vs .map
0.0 0

#1

Tell us what’s happening:

Please tell me what I am doing wrong/

Hi guys, I am a bit confused Why .reduce() doesn’t work here?
Thanks

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(arr => arr > 0  &&  Number.isInteger(arr) )
                             .reduce(arr => arr*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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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


#2

Reduce is for when you want to “reduce” the array to one value, like a sum. Map goes through the array element by element and creates a new array based on some process you apply to each of those elements.

That also isn’t how you use reduce.


#3

So reduce() doesn’t return the values that fits to the condition as an array, right?


#4

You are correct. I believe you are confusing reduce with filter which does what you want.


#5

Thanks, you guys are so fast! Appreciate it!