ES6: Write Higher Order Arrow Functions (Challenge)

Instructions: Use arrow function syntax to compute the square of only the positive integers (decimal numbers are not integers) in the array realNumberArray and store the new array in the variable squaredIntegers .

why does this exercise use the const squaredIntegers declaration twice? Why is my code not working ? it should work. I make sure the element is not a negative number and that it is not a decimal.

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((element) => {
     if(element > 0 && element % 2 === 0) {
        return Math.sqrt(element) ;
  }) ;
  // change code above this line
  return squaredIntegers;
// test your code
const squaredIntegers = squareList(realNumberArray);

@aspiringcoder One is in the global scope and the other is in the squareList function.

You return squareIntegers which was originally assigned a reference to arr (the array passed into the function squareList. You never change the value of squareIntegers inside squareList.

Hint: The filter method returns a new array (does not mutate the array it operates on), but the way you have written your code, the new array is not returned.

EDIT: Also, make sure you understand how the “square” of a number is calculated. You are attempting to calculate the square root which is very different.

1 Like

Another hint: filter is not where you will want to do your math. You will want to add (chain) another function after filter.

Also, “% 2” will eliminate odd integers.

1 Like

ok i will learn about chain right now. thank you for the help!