How to increment a letter using javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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".

20th Apr 2018, 7:02 AM
Adnan Elachola
Adnan Elachola - avatar
3 Answers
+ 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.
20th Apr 2018, 7:40 AM
Mike Choy
Mike Choy - avatar
+ 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
20th Apr 2018, 7:13 AM
syul
syul - avatar
0
thank you mike, syul
20th Apr 2018, 7:50 AM
Adnan Elachola
Adnan Elachola - avatar