Brandon
Brandon

Reputation: 13

Why do I get undefined when using this function?

const array1 = [2,5,10];
    
    const arr10 = function(arr) { arr.map(x => x * 10); };
    
    console.log(arr10(array1));

arr10(array1); should return an array with every number inside array1 * 10. I don't understand why I get undefined.

Upvotes: 0

Views: 75

Answers (4)

Slai
Slai

Reputation: 22886

The confusion might be related to the arrow function cases where return statement can be ommited:

const arr10 = (arr) => arr.map(x => x * 10);

console.log( arr10( [1, 2, 3] ) );

Upvotes: 1

user9255534
user9255534

Reputation:

You don't return anything in your function.

const arr10 = function (arr) {return arr.map((x) => {return x * 10;});}

Upvotes: 0

NullPointer
NullPointer

Reputation: 7368

you have to add return in function:

const array1 = [2,5,10];
const arr10 = function(arr) { return arr.map(x => x * 10); };
console.log(arr10(array1));

Upvotes: 0

mwilson
mwilson

Reputation: 12970

You aren't returning anything in your function:

const arr10 = function(arr) { return arr.map(x => x * 10); };

Upvotes: 0

Related Questions