How to pass numbers to alphabet letters

Asked

Viewed 13,508 times

1

Converts Numbers from 1 ao 26 for A - Z of the Alphabet. I have a directory with several files and wanted to do 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 staff I hope to have expressed myself enough so that we can together find the solution, I count on your help. I thank you, from now on.

  • And what you’ve done to solve your problem?

2 answers

3


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

First, you need to be aware that values from 1 to 26 cannot become letters, because the values of letters in the tabela ascii start at 65, so you must add 64 whenever you want to convert your number to character.

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

And the reverse operation is.

>> 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.

1

Okay, Brumazzi D.B

Thank you for your attention on 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.