kunquan
kunquan

Reputation: 1267

Why doesn't my arrow function return array but instead a function declaration

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?

Upvotes: 0

Views: 76

Answers (6)

Laiso
Laiso

Reputation: 2650

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);

Upvotes: 1

Waqas Noor
Waqas Noor

Reputation: 921

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);

Upvotes: 0

Dmytro Zasiadko
Dmytro Zasiadko

Reputation: 131

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);

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370989

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);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386680

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);

Upvotes: 0

Justinas
Justinas

Reputation: 43507

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

Upvotes: 0

Related Questions