Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need to use a function inside the map of a list

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading