Write Higher Order Arrow Functions_2

Write Higher Order Arrow Functions_2
0

#1

Tell us what’s happening:
I am getting undefined values for all the elements in the new array produced by the MAP function, and I don’t know why. 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.map(e => {if(e>0 && Number.isInteger(e)){Math.pow(e,2)}});
  // change code above this line
  return squaredIntegers;
  
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

OUTPUT:

[ undefined,
  undefined,
  undefined,
  undefined,
  undefined,
  undefined,
  undefined ]

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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


#2

That is because you are not returning any values in the map method’s callback function. Without an explicit/implicit return, a function will return undefined.

Below I put in line returns in your code, but did not modify the actual code written. Now, it should be easier for you to see you are not returning any values inside the callback function.

  const squaredIntegers = arr.map(e => {
    if(e>0 && Number.isInteger(e)){
      Math.pow(e,2)
    }
  });

#3

Thanks. I actually used the following because when I returned the Math.pow(e, 2) I outputted the correct values along with undefined values for the non integer inputs.

 const squaredIntegers = arr.filter(e1 => e1>0 && Number.isInteger(e1)).map(e2 => Math.pow(e2,2));

This passed the assignment.