mamdouh asked this 4 years ago

How to convert character to ascii value in Javascript?

I have to map an array of lowercase characters a to z to numbers 1 to 26.

Example: [a,c,f] to [1,3,6]

I tried function(x){ return (x-96); } )


However this returns an array of NaNs. I do realize i have to convert x to ascii equivalent and then minus 96. How do I do that?

Best Answer by sonja 4 years ago



"a".charCodeAt(0) gives 97 function(x){ return (x.charCodeAt(0)-96); } )