+ 3
How to increment a letter using javascript?
Hi, is there any way to increment a letter using javascript? for example if the letter "A" is given as input we should get the output as "B".
3 Réponses
+ 3
Hi Adnan
Use String.charCodeAt() to get character codes to manipulate. Once you have code you can do maths on it.
Use String.fromCharCode() to convert back.
You can combine them likeso
String.fromCharCode("A".charCodeAt(0) + 1)
//B
You need to be careful for non Unicode characters, where you can use String.fromCodePoint()
sidenote: Strings are immutable, so you need to convert them to arrays in order to make changes to them, and then convert back to string.
+ 3
Disclaimer
I dont javascript
But i can suggest a few ways.
1. You can transfer a string to unicode ex)a= U+0061 , U+0062=b
2. Make a alphabet array and get where the alphabet is located ex)a=1 increment by 1 ->2 get 2nd of alphabet array 2->b
3. Make a decryption array
So all elements aline by 1increased alphabet
ex) a,b,c,d...->b,c,d,e...
Input a-> in array decrypt ->b
0
thank you mike, syul