Function that enumerates letters of the alphabet

Asked

Viewed 8,412 times

9

How to convert alphabet letters into one array of numbers according to its sequence?

The array should start from paragraph 1, not zero. So the letter A valeria 1, B valeria 2, C valeria 3, and so on.

I will pass as argument the letters in sequence without spaces or any type of delimiter.

Letters can be passed in any set, e.g.: "ac" (should result in [1, 3]); or "ca" (must result in [3, 1]).

It is possible to work with other types of alphabet, such as Russian: А valeria 1; Б valeria 2; В valeria 3; Г valeria 4, and so on.

6 answers

7

Another alternative that can be used is:

function enumerarLetras(texto){
    texto = texto.toLowerCase();
    var numeros = [];
    texto.split('').map(function(letra){
        numeros.push((letra.charCodeAt(0) - 97) + 1);
    });
    return numeros;
}

alert(enumerarLetras('ACBED'));

Note: The above function works only in cases where the text has no accents, it works only with the characters of the latin alphabet.

The code below was based on Answer from the Tobymosque and in the OS.

var Latinise={};Latinise.latin_map={"Á":"A","Ă":"A","Ắ":"A","Ặ":"A","Ằ":"A","Ẳ":"A","Ẵ":"A","Ǎ":"A","Â":"A","Ấ":"A","Ậ":"A","Ầ":"A","Ẩ":"A","Ẫ":"A","Ä":"A","Ǟ":"A","Ȧ":"A","Ǡ":"A","Ạ":"A","Ȁ":"A","À":"A","Ả":"A","Ȃ":"A","Ā":"A","Ą":"A","Å":"A","Ǻ":"A","Ḁ":"A","Ⱥ":"A","Ã":"A","Ꜳ":"AA","Æ":"AE","Ǽ":"AE","Ǣ":"AE","Ꜵ":"AO","Ꜷ":"AU","Ꜹ":"AV","Ꜻ":"AV","Ꜽ":"AY","Ḃ":"B","Ḅ":"B","Ɓ":"B","Ḇ":"B","Ƀ":"B","Ƃ":"B","Ć":"C","Č":"C","Ç":"C","Ḉ":"C","Ĉ":"C","Ċ":"C","Ƈ":"C","Ȼ":"C","Ď":"D","Ḑ":"D","Ḓ":"D","Ḋ":"D","Ḍ":"D","Ɗ":"D","Ḏ":"D","Dz":"D","Dž":"D","Đ":"D","Ƌ":"D","DZ":"DZ","DŽ":"DZ","É":"E","Ĕ":"E","Ě":"E","Ȩ":"E","Ḝ":"E","Ê":"E","Ế":"E","Ệ":"E","Ề":"E","Ể":"E","Ễ":"E","Ḙ":"E","Ë":"E","Ė":"E","Ẹ":"E","Ȅ":"E","È":"E","Ẻ":"E","Ȇ":"E","Ē":"E","Ḗ":"E","Ḕ":"E","Ę":"E","Ɇ":"E","Ẽ":"E","Ḛ":"E","Ꝫ":"ET","Ḟ":"F","Ƒ":"F","Ǵ":"G","Ğ":"G","Ǧ":"G","Ģ":"G","Ĝ":"G","Ġ":"G","Ɠ":"G","Ḡ":"G","Ǥ":"G","Ḫ":"H","Ȟ":"H","Ḩ":"H","Ĥ":"H","Ⱨ":"H","Ḧ":"H","Ḣ":"H","Ḥ":"H","Ħ":"H","Í":"I","Ĭ":"I","Ǐ":"I","Î":"I","Ï":"I","Ḯ":"I","İ":"I","Ị":"I","Ȉ":"I","Ì":"I","Ỉ":"I","Ȋ":"I","Ī":"I","Į":"I","Ɨ":"I","Ĩ":"I","Ḭ":"I","Ꝺ":"D","Ꝼ":"F","Ᵹ":"G","Ꞃ":"R","Ꞅ":"S","Ꞇ":"T","Ꝭ":"IS","Ĵ":"J","Ɉ":"J","Ḱ":"K","Ǩ":"K","Ķ":"K","Ⱪ":"K","Ꝃ":"K","Ḳ":"K","Ƙ":"K","Ḵ":"K","Ꝁ":"K","Ꝅ":"K","Ĺ":"L","Ƚ":"L","Ľ":"L","Ļ":"L","Ḽ":"L","Ḷ":"L","Ḹ":"L","Ⱡ":"L","Ꝉ":"L","Ḻ":"L","Ŀ":"L","Ɫ":"L","Lj":"L","Ł":"L","LJ":"LJ","Ḿ":"M","Ṁ":"M","Ṃ":"M","Ɱ":"M","Ń":"N","Ň":"N","Ņ":"N","Ṋ":"N","Ṅ":"N","Ṇ":"N","Ǹ":"N","Ɲ":"N","Ṉ":"N","Ƞ":"N","Nj":"N","Ñ":"N","NJ":"NJ","Ó":"O","Ŏ":"O","Ǒ":"O","Ô":"O","Ố":"O","Ộ":"O","Ồ":"O","Ổ":"O","Ỗ":"O","Ö":"O","Ȫ":"O","Ȯ":"O","Ȱ":"O","Ọ":"O","Ő":"O","Ȍ":"O","Ò":"O","Ỏ":"O","Ơ":"O","Ớ":"O","Ợ":"O","Ờ":"O","Ở":"O","Ỡ":"O","Ȏ":"O","Ꝋ":"O","Ꝍ":"O","Ō":"O","Ṓ":"O","Ṑ":"O","Ɵ":"O","Ǫ":"O","Ǭ":"O","Ø":"O","Ǿ":"O","Õ":"O","Ṍ":"O","Ṏ":"O","Ȭ":"O","Ƣ":"OI","Ꝏ":"OO","Ɛ":"E","Ɔ":"O","Ȣ":"OU","Ṕ":"P","Ṗ":"P","Ꝓ":"P","Ƥ":"P","Ꝕ":"P","Ᵽ":"P","Ꝑ":"P","Ꝙ":"Q","Ꝗ":"Q","Ŕ":"R","Ř":"R","Ŗ":"R","Ṙ":"R","Ṛ":"R","Ṝ":"R","Ȑ":"R","Ȓ":"R","Ṟ":"R","Ɍ":"R","Ɽ":"R","Ꜿ":"C","Ǝ":"E","Ś":"S","Ṥ":"S","Š":"S","Ṧ":"S","Ş":"S","Ŝ":"S","Ș":"S","Ṡ":"S","Ṣ":"S","Ṩ":"S","Ť":"T","Ţ":"T","Ṱ":"T","Ț":"T","Ⱦ":"T","Ṫ":"T","Ṭ":"T","Ƭ":"T","Ṯ":"T","Ʈ":"T","Ŧ":"T","Ɐ":"A","Ꞁ":"L","Ɯ":"M","Ʌ":"V","Ꜩ":"TZ","Ú":"U","Ŭ":"U","Ǔ":"U","Û":"U","Ṷ":"U","Ü":"U","Ǘ":"U","Ǚ":"U","Ǜ":"U","Ǖ":"U","Ṳ":"U","Ụ":"U","Ű":"U","Ȕ":"U","Ù":"U","Ủ":"U","Ư":"U","Ứ":"U","Ự":"U","Ừ":"U","Ử":"U","Ữ":"U","Ȗ":"U","Ū":"U","Ṻ":"U","Ų":"U","Ů":"U","Ũ":"U","Ṹ":"U","Ṵ":"U","Ꝟ":"V","Ṿ":"V","Ʋ":"V","Ṽ":"V","Ꝡ":"VY","Ẃ":"W","Ŵ":"W","Ẅ":"W","Ẇ":"W","Ẉ":"W","Ẁ":"W","Ⱳ":"W","Ẍ":"X","Ẋ":"X","Ý":"Y","Ŷ":"Y","Ÿ":"Y","Ẏ":"Y","Ỵ":"Y","Ỳ":"Y","Ƴ":"Y","Ỷ":"Y","Ỿ":"Y","Ȳ":"Y","Ɏ":"Y","Ỹ":"Y","Ź":"Z","Ž":"Z","Ẑ":"Z","Ⱬ":"Z","Ż":"Z","Ẓ":"Z","Ȥ":"Z","Ẕ":"Z","Ƶ":"Z","IJ":"IJ","Œ":"OE","ᴀ":"A","ᴁ":"AE","ʙ":"B","ᴃ":"B","ᴄ":"C","ᴅ":"D","ᴇ":"E","ꜰ":"F","ɢ":"G","ʛ":"G","ʜ":"H","ɪ":"I","ʁ":"R","ᴊ":"J","ᴋ":"K","ʟ":"L","ᴌ":"L","ᴍ":"M","ɴ":"N","ᴏ":"O","ɶ":"OE","ᴐ":"O","ᴕ":"OU","ᴘ":"P","ʀ":"R","ᴎ":"N","ᴙ":"R","ꜱ":"S","ᴛ":"T","ⱻ":"E","ᴚ":"R","ᴜ":"U","ᴠ":"V","ᴡ":"W","ʏ":"Y","ᴢ":"Z","á":"a","ă":"a","ắ":"a","ặ":"a","ằ":"a","ẳ":"a","ẵ":"a","ǎ":"a","â":"a","ấ":"a","ậ":"a","ầ":"a","ẩ":"a","ẫ":"a","ä":"a","ǟ":"a","ȧ":"a","ǡ":"a","ạ":"a","ȁ":"a","à":"a","ả":"a","ȃ":"a","ā":"a","ą":"a","ᶏ":"a","ẚ":"a","å":"a","ǻ":"a","ḁ":"a","ⱥ":"a","ã":"a","ꜳ":"aa","æ":"ae","ǽ":"ae","ǣ":"ae","ꜵ":"ao","ꜷ":"au","ꜹ":"av","ꜻ":"av","ꜽ":"ay","ḃ":"b","ḅ":"b","ɓ":"b","ḇ":"b","ᵬ":"b","ᶀ":"b","ƀ":"b","ƃ":"b","ɵ":"o","ć":"c","č":"c","ç":"c","ḉ":"c","ĉ":"c","ɕ":"c","ċ":"c","ƈ":"c","ȼ":"c","ď":"d","ḑ":"d","ḓ":"d","ȡ":"d","ḋ":"d","ḍ":"d","ɗ":"d","ᶑ":"d","ḏ":"d","ᵭ":"d","ᶁ":"d","đ":"d","ɖ":"d","ƌ":"d","ı":"i","ȷ":"j","ɟ":"j","ʄ":"j","dz":"dz","dž":"dz","é":"e","ĕ":"e","ě":"e","ȩ":"e","ḝ":"e","ê":"e","ế":"e","ệ":"e","ề":"e","ể":"e","ễ":"e","ḙ":"e","ë":"e","ė":"e","ẹ":"e","ȅ":"e","è":"e","ẻ":"e","ȇ":"e","ē":"e","ḗ":"e","ḕ":"e","ⱸ":"e","ę":"e","ᶒ":"e","ɇ":"e","ẽ":"e","ḛ":"e","ꝫ":"et","ḟ":"f","ƒ":"f","ᵮ":"f","ᶂ":"f","ǵ":"g","ğ":"g","ǧ":"g","ģ":"g","ĝ":"g","ġ":"g","ɠ":"g","ḡ":"g","ᶃ":"g","ǥ":"g","ḫ":"h","ȟ":"h","ḩ":"h","ĥ":"h","ⱨ":"h","ḧ":"h","ḣ":"h","ḥ":"h","ɦ":"h","ẖ":"h","ħ":"h","ƕ":"hv","í":"i","ĭ":"i","ǐ":"i","î":"i","ï":"i","ḯ":"i","ị":"i","ȉ":"i","ì":"i","ỉ":"i","ȋ":"i","ī":"i","į":"i","ᶖ":"i","ɨ":"i","ĩ":"i","ḭ":"i","ꝺ":"d","ꝼ":"f","ᵹ":"g","ꞃ":"r","ꞅ":"s","ꞇ":"t","ꝭ":"is","ǰ":"j","ĵ":"j","ʝ":"j","ɉ":"j","ḱ":"k","ǩ":"k","ķ":"k","ⱪ":"k","ꝃ":"k","ḳ":"k","ƙ":"k","ḵ":"k","ᶄ":"k","ꝁ":"k","ꝅ":"k","ĺ":"l","ƚ":"l","ɬ":"l","ľ":"l","ļ":"l","ḽ":"l","ȴ":"l","ḷ":"l","ḹ":"l","ⱡ":"l","ꝉ":"l","ḻ":"l","ŀ":"l","ɫ":"l","ᶅ":"l","ɭ":"l","ł":"l","lj":"lj","ſ":"s","ẜ":"s","ẛ":"s","ẝ":"s","ḿ":"m","ṁ":"m","ṃ":"m","ɱ":"m","ᵯ":"m","ᶆ":"m","ń":"n","ň":"n","ņ":"n","ṋ":"n","ȵ":"n","ṅ":"n","ṇ":"n","ǹ":"n","ɲ":"n","ṉ":"n","ƞ":"n","ᵰ":"n","ᶇ":"n","ɳ":"n","ñ":"n","nj":"nj","ó":"o","ŏ":"o","ǒ":"o","ô":"o","ố":"o","ộ":"o","ồ":"o","ổ":"o","ỗ":"o","ö":"o","ȫ":"o","ȯ":"o","ȱ":"o","ọ":"o","ő":"o","ȍ":"o","ò":"o","ỏ":"o","ơ":"o","ớ":"o","ợ":"o","ờ":"o","ở":"o","ỡ":"o","ȏ":"o","ꝋ":"o","ꝍ":"o","ⱺ":"o","ō":"o","ṓ":"o","ṑ":"o","ǫ":"o","ǭ":"o","ø":"o","ǿ":"o","õ":"o","ṍ":"o","ṏ":"o","ȭ":"o","ƣ":"oi","ꝏ":"oo","ɛ":"e","ᶓ":"e","ɔ":"o","ᶗ":"o","ȣ":"ou","ṕ":"p","ṗ":"p","ꝓ":"p","ƥ":"p","ᵱ":"p","ᶈ":"p","ꝕ":"p","ᵽ":"p","ꝑ":"p","ꝙ":"q","ʠ":"q","ɋ":"q","ꝗ":"q","ŕ":"r","ř":"r","ŗ":"r","ṙ":"r","ṛ":"r","ṝ":"r","ȑ":"r","ɾ":"r","ᵳ":"r","ȓ":"r","ṟ":"r","ɼ":"r","ᵲ":"r","ᶉ":"r","ɍ":"r","ɽ":"r","ↄ":"c","ꜿ":"c","ɘ":"e","ɿ":"r","ś":"s","ṥ":"s","š":"s","ṧ":"s","ş":"s","ŝ":"s","ș":"s","ṡ":"s","ṣ":"s","ṩ":"s","ʂ":"s","ᵴ":"s","ᶊ":"s","ȿ":"s","ɡ":"g","ᴑ":"o","ᴓ":"o","ᴝ":"u","ť":"t","ţ":"t","ṱ":"t","ț":"t","ȶ":"t","ẗ":"t","ⱦ":"t","ṫ":"t","ṭ":"t","ƭ":"t","ṯ":"t","ᵵ":"t","ƫ":"t","ʈ":"t","ŧ":"t","ᵺ":"th","ɐ":"a","ᴂ":"ae","ǝ":"e","ᵷ":"g","ɥ":"h","ʮ":"h","ʯ":"h","ᴉ":"i","ʞ":"k","ꞁ":"l","ɯ":"m","ɰ":"m","ᴔ":"oe","ɹ":"r","ɻ":"r","ɺ":"r","ⱹ":"r","ʇ":"t","ʌ":"v","ʍ":"w","ʎ":"y","ꜩ":"tz","ú":"u","ŭ":"u","ǔ":"u","û":"u","ṷ":"u","ü":"u","ǘ":"u","ǚ":"u","ǜ":"u","ǖ":"u","ṳ":"u","ụ":"u","ű":"u","ȕ":"u","ù":"u","ủ":"u","ư":"u","ứ":"u","ự":"u","ừ":"u","ử":"u","ữ":"u","ȗ":"u","ū":"u","ṻ":"u","ų":"u","ᶙ":"u","ů":"u","ũ":"u","ṹ":"u","ṵ":"u","ᵫ":"ue","ꝸ":"um","ⱴ":"v","ꝟ":"v","ṿ":"v","ʋ":"v","ᶌ":"v","ⱱ":"v","ṽ":"v","ꝡ":"vy","ẃ":"w","ŵ":"w","ẅ":"w","ẇ":"w","ẉ":"w","ẁ":"w","ⱳ":"w","ẘ":"w","ẍ":"x","ẋ":"x","ᶍ":"x","ý":"y","ŷ":"y","ÿ":"y","ẏ":"y","ỵ":"y","ỳ":"y","ƴ":"y","ỷ":"y","ỿ":"y","ȳ":"y","ẙ":"y","ɏ":"y","ỹ":"y","ź":"z","ž":"z","ẑ":"z","ʑ":"z","ⱬ":"z","ż":"z","ẓ":"z","ȥ":"z","ẕ":"z","ᵶ":"z","ᶎ":"z","ʐ":"z","ƶ":"z","ɀ":"z","ff":"ff","ffi":"ffi","ffl":"ffl","fi":"fi","fl":"fl","ij":"ij","œ":"oe","st":"st","ₐ":"a","ₑ":"e","ᵢ":"i","ⱼ":"j","ₒ":"o","ᵣ":"r","ᵤ":"u","ᵥ":"v","ₓ":"x","ί":"ι"};

String.prototype.latinise = function(){
    return this.replace(/[^A-Za-z0-9\[\] ]/g, function(a){
        return Latinise.latin_map[a]||a
    })
};
String.prototype.latinize=String.prototype.latinise;
                                                                     
function enumerarLetras(texto){
   var str = texto.latinize().replace(/\s+/g, '');
   var numeros = [];
   str.split('').map(function(letra){
      var numero = letra.charCodeAt();
      switch(true){
         case (numero >= 97 && numero <= 122):    // Latin - letras minúsculas
            numero -= 96;
            break;
         case (numero >= 65 && numero <= 90):     // Latin - letras MAIÚSCULAS
            numero -= 64;
            break;
         case (numero >= 1072 && numero <= 1103): // Russo - letras minúsculas
            numero -= 1071;
            break;
         case (numero >= 1040 && numero <= 1071): // Russo - letras MAIÚSCULAS
            numero -= 1039;
            break;
         case (numero >= 945 && numero <= 969):   // Grego - letras minúsculas
            numero -= 944;
            break;
         case (numero >= 913 && numero <= 937):   // Grego - letras MAIÚSCULAS
            numero -= 912;
            break;                                    
      }                                                                                                                                           
      numeros.push(numero);
    });
    return numeros;
}

alert(enumerarLetras("stack Overflow"));
alert(enumerarLetras("переполнение стека"));
alert(enumerarLetras("Υπερχείλιση στοίβας"));

6


You need to build a table with your alphabet. You can put the letters you want from any alphabet in the order you want. Then just do what you call lookup in this table.

function converte(letras) {
    var alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var codigos = [];
    for (var i in letras) {
        codigos.push(alfabeto.indexOf(letras[i].toUpperCase()) + 1);
    }
    return codigos;
}

var resultado = converte("acdA");
for (var i in resultado) {
    console.log(resultado[i]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In this solution, you can put the alphabet at ease, any alphabet, in the order you want, you can even invent something. The important thing is to place the desired character in the position of the array which wishes the result (whereas array starts at zero, so the algorithm makes the hit to 1). One could also leave the first element of the array as something null and useless, then the algorithm would not have to add a.

Alternative

If you have to consider accented characters as the same character, you have to add a function to normalize the string. If anyone needs:

function converte(letras) {
    let alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let codigos = [];
    for (let i in letras) codigos.push(alfabeto.indexOf(removeAcento(letras[i].toUpperCase())) + 1);
    return codigos;
}

function removeAcento(letra) {
    let acentos = {
    "ÁÅÃÀÂÄ" : "A",
    "ÉÊÈË" : "E",
    "ÍÎÌÏ" : "I",
    "ÓÕÒÔÖ" : "O",
    "ÚÙÛÜ" : "U",
    "Ç" : "C"
    };
    for (let key in acentos) {
        for (let a = 0; a < key.length; a++) if (letra === key[a]) return acentos[key];
    }
    return letra;
}

let resultado = converte("acdAé");
for (let i in resultado) console.log(resultado[i]);

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you need to use other alphabets - not just Cyrillic or Greek, just change the letters used.

  • I liked your solution, but in firefox Console informs the following error SyntaxError: missing ( after for, I noticed that when I removed the each in front of the for does not give more error, only that does not work as expected. I also did not understand the reason of the second for each, out of function. The expected result has to be something like: console.log(converte('acbed')); = Array [ 1, 3, 2, 5, 4 ], but it seems that the Ideone Site link is a little different. Could perform such modifications, please?

  • 1

    @Eden I modified. I’m sorry I use many languages, sometimes confusing them. It worked on a JS implementation but it doesn’t work at all. Now I used a way that works in all. It doesn’t look so elegant but the priority is to work. The print is necessary in ideone because it does not have comsole.log, but just swap one for the other. I used the second for to display the result, i.e., print item by item of the array.To get the result you do not need to do the last 3 lines, but without them you cannot print the result. If you need to format the printing as you want, then just adapt

  • @bigown - It was perfect! It works exactly as I expected, you mentioned above the "function to normalize the string", I would be very grateful if you could add this functionality, then I end accepting your answer as the most useful. Thank you so much for your help!!!

  • @Eden you need to be disregarded if you are in upper and lower case and treat everything as a spo thing? For example A and a would be worth 1? And even if you have accent, ie if you find á or ã also worth 1? Need to make this clear the criterion (it would be good to edit the question to clarify this).

  • @bigown - There should be no difference between upper and lower case letters, both would be equivalent (A and a would be worth 1), but accented characters should not have the same value (á and ã WOULD NOT BE WORTH 1).

  • @Eden see if it’s ok now.

  • @bigown - It worked very well, thank you very much for this further modification.

Show 2 more comments

6

An alternative is to analyze the charCode of each character.

var getNumeros = function (palavra) {
    var numeros = [];
    for (var i = 0; i < palavra.length; ++i)
    {
        //Latin - minusculo
        var numero = palavra.charCodeAt(i);
        if (numero >= 65 && numero <= 90)
            numero -= 64;
        
        //Latin - maisculo
        if (numero >= 97 && numero <= 122)
            numero -= 96;
            
        //Russo - minusculo
        if (numero >= 1040 && numero <= 1071)
            numero -= 1039;
        
        //Russo - maisculo
        if (numero >= 1072 && numero <= 1103)
            numero -= 1071;
            
        numeros.push(numero);
    }
    return numeros;
}

var content = document.getElementById("content");
var createDiv = function(palavra) {    
    var div = document.createElement('h1');
    div.innerHTML = palavra + ' => ' + getNumeros(palavra);
    content.appendChild(div);
}

createDiv('abyz');
createDiv('ABYZ');
createDiv('абюя');
createDiv('АБЮЯ');
<div id="content">
    
</div>

In the above example, I’m checking for minuscule and uppercase letters, both from Latin and Russian. The above code will not work for words that have accents/diacritics, in which case it will be necessary to remove all.

if you need to do so, you can look at the following topic on Soen - Javascript, Remove-Accents/Diacritics in Strings:

  • Your comments on the code are reversed. =)

5

Here is a way that knows how to treat accents and works with the Latin, Cyrillic and Greek alphabets:

var latinoMaiusculo = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var latinoMinusculo = "abcdefghijklmnopqrstuvwxyz";
var cirilicoMaiusculo = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
var cirilicoMinusculo = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
var gregoMaiusculo = "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ";
var gregoMinusculo = "αβγδεζηθικλμνξοπρστυφχψω";

function eliminarAcento(letra) {
  var acentos = [
    {de: "áåãàâä", para: "a"},
    {de: "ÁÅÃÀÂÄ", para: "A"},
    {de: "éêèë", para: "e"},
    {de: "ÉÊÈË", para: "E"},
    {de: "íîìï", para: "i"},
    {de: "ÍÎÌÏ", para: "I"},
    {de: "óõòôö", para: "o"},
    {de: "ÓÕÒÔÖ", para: "O"},
    {de: "úùûü", para: "u"},
    {de: "ÚÙÛÜ", para: "U"},
    {de: "ý", para: "y"},
    {de: "Ý", para: "Y"},
    {de: "ç", para: "c"},
    {de: "Ç", para: "C"},
    {de: "ñ", para: "n"},
    {de: "Ñ", para: "N"},
    {de: "ά", para: "α"},
    {de: "έ", para: "ε"},
    {de: "ή", para: "η"},
    {de: "ίϊ", para: "ι"},
    {de: "ό", para: "ο"},
    {de: "ύϋ", para: "υ"},
    {de: "ώ", para: "ω"},
    {de: "Ά", para: "Α"},
    {de: "Έ", para: "Ε"},
    {de: "Ή", para: "Η"},
    {de: "ΊΪ", para: "Ι"},
    {de: "Ό", para: "Ο"},
    {de: "ΎΫ", para: "Υ"},
    {de: "Ώ", para: "Ω"},
    {de: "ς", para: "σ"}
  ];
  for (e in acentos) {
    for (var a = 0; a < acentos[e].de.length; a++) {
      if (letra === acentos[e].de[a]) return acentos[e].para;
    }
  }
  return letra;
}

function enumerar(texto) {
  var array = [];
  for (var i = 0; i < texto.length; i++) {
    var letra = eliminarAcento(texto[i]);
    var pos = latinoMaiusculo.indexOf(letra);
    if (pos === -1) pos = latinoMinusculo.indexOf(letra);
    if (pos === -1) pos = cirilicoMaiusculo.indexOf(letra);
    if (pos === -1) pos = cirilicoMinusculo.indexOf(letra);
    if (pos === -1) pos = gregoMaiusculo.indexOf(letra);
    if (pos === -1) pos = gregoMinusculo.indexOf(letra);
    array.push(pos + 1);
  }
  return array;
}

alert(enumerar("Conceição Хорошо Ελληνικό"));

4

You can take advantage of the fact that, in Unicode, the letters are usually already encoded in the same sequence as they appear in the day-to-day alphabets. This is not guaranteed, however, so I suggest checking the relevant Unicode tables and - if you notice any discrepancy between what you want and what Unicode provides (difficult, but possible) - then specifying an alphabet yourself (such as suggested by Maniero).

For example, in ASCII (subset of Unicode) the letter A (capital) has the code point 65 (0x41 in hexadecimal), the B 66 (0x42), the C 67 (0x43) and so on. So you just get the letter code - using charCodeAt (in Ecmascript 6 there will also be codePointAt, but unless you want to work with more unusual characters - like traditional Chinese - you don’t need it) - and subtract 0x40 (1 less than the letter A) to turn each letter into the desired code:

var str = "ABRACADABRA";
for ( var i = 0 ; i < str.length ; i++ ) {
  var cod = str.charCodeAt(i) - 0x40;
  document.getElementsByTagName("body")[0].innerHTML += "<p>" + cod + "</p>";
}

Alphabetically Cyrillic (used in the Russian language), the base letter is А, code point 0x0410. Б is 0x0411, В is 0x0412 and so on:

var str = "АБГ";
for ( var i = 0 ; i < str.length ; i++ ) {
  var cod = str.charCodeAt(i) - 0x040f;
  document.getElementsByTagName("body")[0].innerHTML += "<p>" + cod + "</p>";
}

Note: the code of my reply distinguishes between upper and lower case; if you do not want this distinction, transform the string in block capitals (by toUpperCase) before using it. Otherwise, for reference the a lowercase has code 97 (0x61) and the а cyrillic has code 0x0430.

0

I needed it for a job and I did it in a very simple way I leave it here for those who need help.

 <script>
    var x = prompt("digite uma letra");

    if (isNaN(x) == false) {

        alert("nao é uma letra");
    } else {

        var alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";



        //  transforma em maiusculo e joga em outra variavel, pois a variavel original nao é alterada
        var y = x.toUpperCase();
        // pega a posição da letra no alfabeto, a posição começa em 0 por isso transformo em int para somar +1

        var conta = parseInt(alfabeto.indexOf(y));
        alert(conta + 1);


    }
</script>

Browser other questions tagged

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