0

I tried to write a arrow function to compute the square of only the positive integers ( not include the fractions).

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
    "use strict";

    const squaredIntegers = (arr) => {
        let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele));
        return arrayChoosen.map(x => x * x);
    }

    return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

But the result is a function declaration, not a array as I expect. But when I try to modify the code in this way

const squareList = (arr) => {
  "use strict";
  let squaredIntegers = arr.filter(ele => ele > 0 && Number.isInteger(ele));
  squaredIntegers = squaredIntegers.map(val => val * val);
  return squaredIntegers;
};

Then it output the array I expect. Why in the first case it doesn't work?

1
  • In the first example the function squaredIntegers() is never called, but only the function reference is returned. So why would you expect the result to be an array in the first place? Commented Aug 2, 2018 at 6:39

6 Answers 6

1

You need to return the returned value of your squaredIntegers function, so try with this code:

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
    "use strict";

    const squaredIntegers = (a) => {
        let arrayChoosen = a.filter(ele => ele > 0 && Number.isInteger(ele));
        return arrayChoosen.map(x => x * x);
    }

    return squaredIntegers(arr);
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Sign up to request clarification or add additional context in comments.

1 Comment

Although you have fixed OPs code so that it works, your answer neglects to explain why their code didn't work, which was actually the original question.
0

Because you return function name only - that is - reference to function.

Instead, you should call that function:

return squaredIntegers();

Your second example works fine because you are actually calling arr.filter() and assign it's value to squaredIntegers

1 Comment

Thank you. You help me clear the concept of arrow function
0

You need to call squaredIntegers with arr for getting a new array.

return squaredIntegers(arr);

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
    "use strict";

    const squaredIntegers = (arr) => {
        let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele));
        return arrayChoosen.map(x => x * x);
    }

    return squaredIntegers(arr);
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Comments

0

Because the first squareList returns the function itself, rather than the function called on the input arr. You might return its invocation instead:

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
    "use strict";

    const squaredIntegers = (arr) => {
        let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele));
        return arrayChoosen.map(x => x * x);
    }

    return squaredIntegers(arr);
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Comments

0

Because you return a function when you need to return result of the function call. Check the corrected code.

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
    "use strict";

    const squaredIntegers = (arr) => {
        let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele));
        return arrayChoosen.map(x => x * x);
    }

    return squaredIntegers(arr);
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Comments

0

You are returning a function rather than a function call.

Change return squaredIntegers; to return squaredIntegers(arr);

So it will become

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
    "use strict";

    const squaredIntegers = (arr) => {
        let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele));
        return arrayChoosen.map(x => x * x);
    }

    return squaredIntegers(arr);
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.