How to pass numbers to alphabet letters

1

Converts numbers from 1 ao 26 to A - Z of the Alphabet. I have a directory with several files and I wanted to make a "FOR" for, ... the name (file). I'm doing this with files named by ordinal numbers, but I'd like to do this with files named by letters.

<script>

    var dir = "video"; // acessa o diretório vídeos, onde estão os vídeos .3gp e suas thumnail .png
window.onload = function(){
    var alfabeto = "3";
for (i = 1; i <= alfabeto; i++) {
        document.body.innerHTML += "<a href='"+dir+"/"+i+".3gp' target='player'><img src='"+dir+"/"+i+".png' class='option'></a>";
    }
}
</script>

Good people, I hope I have expressed enough that we can find the solution together, I count on your help. Thank you in advance.

    
asked by anonymous 27.02.2016 / 02:19

2 answers

4

As with any language, javascript can also work with numeric character values, of course not as well as in C and C ++.

First of all, you need to be aware that values from 1 to 26 can not be letters, since the values of the letters in tabela ascii start at 65, so you must add 64 whenever you convert your number to

>> var char_A = 1;
>> console.log(String.fromCharCode(char_A+64))
'A'

And the reverse operation stays.

>> var num_1 = 'A';
>> console.log(num_1.charCodeAt(0)-64)
1

The parameter passed in charCodeAt , represents the position of the character in the string.

    
27.02.2016 / 07:08
1

Ok, Brumazzi D.B

Thank you for your attention to my question.

Done! I leave here the code for future research by other users.

<script>

    var dir = "video";
    window.onload = function(){
    alfabeto = 26
    for (i = 1; i <= alfabeto; i++) {
    var cod = (String.fromCharCode(i + 64));
    document.body.innerHTML += "<a href='"+dir+"/"+cod+".3gp' target='player'><img src='"+dir+"/"+cod+".png' boder='0'/></a>";
    }
}

</script>
    
27.02.2016 / 16:06