I'm just learning JavaScript and I'm trying to do the following. Let's say I have the following function:
f(a,b) {return(a b)}
And now I have a list of for example the following:
list = [1,2,3]
What I would like to do is map for that list in which I call the function above f
with a predefined value and each value of the list. I've tried this:
listB = list.map(x => f(x 23))
But it returns a NaN
, and if I add some brackets that snippet returns undefined
.
What's going on and how can I solve it?
CodePudding user response:
Should be f(x, 23)
instead of f(x 23)
:
function f(a,b) {return(a b)}
listA = [1,2,3]
listB = listA.map(x => f(x,23))
console.log(listB)
CodePudding user response:
you have error in your function call f(x 23)
. This is because your function f
expects 2 arguments, and you forward only one. number undefined -> NaN
CodePudding user response:
you can re-write it as
const list = [1, 2, 3, 4];
const listb = list.map(f);
function f(num) {
return num 23;
}