Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert number to letter javascript

Tags:

I'm trying to convert numbers into letters. I'm making an array of divs that either need a number or a number and letter. so 1-3 are just 1-3. but 4-13 need to be a/4, b/5, c6 and so on. is there a way I can converts these numbers into letter easily. maybe changing the ascii values by a set amount?

     for(var i = 1; i < 33; i++){
    if( i < 4 || (i > 13 && i < 20) || i > 29){
        $('#teeth-diagram').append("<div class='tooth' id='" + i + "'>&nbsp;</div>");
    }else{
        $('#teeth-diagram').append("<div class='tooth' id='" + Letter goes here + "/" + i + "'>&nbsp;</div>");
    }
}
like image 555
Gambai Avatar asked Nov 02 '12 19:11

Gambai


2 Answers

since 97 is the ascii value for 'a', and your value for 'a' is 3, you need to do this to get the value of the integer converted to a character:

if(i>=3){
    String.fromCharCode(94 + i);
}
like image 177
Fisch Avatar answered Oct 15 '22 22:10

Fisch


Yes, you can. Use var letter = String.fromCharCode(number); To get a lowercase a, the number would be 97, b would be 98 and so forth. For uppercase A 65, B would be 66 and so forth. See this JSFiddle for an example

like image 40
Lukas_Skywalker Avatar answered Oct 15 '22 20:10

Lukas_Skywalker