Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Find out previous letter in alphabet

Tags:

javascript

If I got a Letter in JavaScript, I'd like to find out the previous letter in alphabetic order, so if input is "C", output must be "B". Are there any standard solutions or do i have to create some special functions?

like image 444
Florian Müller Avatar asked Nov 04 '10 09:11

Florian Müller


People also ask

How do you check if the first character of a string is a letter Javascript?

To check if a character is a letter, call the test() method on the following regular expression - /^[a-zA-Z]+$/ . If the character is a letter, the test method will return true , otherwise false will be returned. Copied!

How do you get the next letter in Javascript?

To get the next letter of the alphabet in JavaScript, we can use the String. fromCharCode static string method and the charCodeAt string instance method.


1 Answers

var ch = 'b';
String.fromCharCode(ch.charCodeAt(0) - 1); // 'a'

And if you wanted to loop around the alphabet just do a check specifically for 'a' -- loop to 'z' if it is, otherwise use the method above.

like image 57
Ben Lee Avatar answered Nov 06 '22 15:11

Ben Lee