mamdouh asked this 2 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 

arr.map( 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?


sonja 2 years ago
1 like

Use 

string.charCodeAt(0)

"a".charCodeAt(0) gives 97

arr.map( function(x){ return (x.charCodeAt(0)-96); } )