Function that lists letters of the alphabet

8

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

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

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

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

You can work with other types of alphabet, such as Russian: А valeria 1 ; Б would be 2 ; В would be 3 ; Г would be 4 , and so on.

    
asked by anonymous 15.03.2015 / 17:01

6 answers

5

You need to set up a table with your alphabet. You can put the letters you want from any alphabet, in any order you want. Then just do what is called 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]);
}

See running on ideone .

In this solution, you can put the alphabet at ease, any alphabet, in whatever order you want, can even invent something. The important thing is to put the desired character in the array position that desires the result (assuming that in the array starts at zero, so the algorithm does the correct 1). You could also leave the first element of array as something null and useless, then the algorithm would not have to add one.

Alternative

If you have to consider accented characters to be the same character, you have to add a function to normalize the string . If someone needs it:

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

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

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

See running on ideone .

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

    
15.03.2015 / 17:24
6

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

The code below was based on the TobyMosque response and in the SO .

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("Υπερχείλιση στοίβας"));
    
15.03.2015 / 17:58
5

Here is a form that knows how to handle accents and works with 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 Хорошо Ελληνικό"));
    
15.03.2015 / 18:05
5

An alternative is to parse 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 example above, I'm checking for lowercase and uppercase, both Latin and Russian. The code above will not work for words that have accents / diacritics, in which case you will need to remove all.

If you need to do this, you can look at the following topic in the SOen-Javascript, Remove -Accents / Diacritics in Strings :

    
15.03.2015 / 17:57
4

You can take advantage of the fact that, in Unicode, the letters usually come encoded in the same sequence that they appear in the alphabets of the day-to-day. 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 specify an alphabet yourself (such as suggested by bigown ).

As an example, in ASCII (Unicode subset) the letter A (uppercase) has the < in> code point 65 ( 0x41 in hexadecima), B 66 ( 0x42 ), C 67 ( 0x43 ), and so on. So you just get the code for the letter - using charCodeAt (in EcmaScript 6 there will also be codePointAt , but unless you want to work with more unusual characters - such as Traditional Chinese - you do not need it) - and subtract 0x40 (1 less than the letter A ) to transform each letter into the desired code: p>

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>";
}

For the alphabet Cyrillic (used in Russian), 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: My response code is case-sensitive; if you do not want this distinction, turn the entire string into uppercase (via toUpperCase ) before using it. Otherwise, for reference a lowercase has code 97 ( 0x61 ) and а Cyrillic has code 0x0430 .

    
15.03.2015 / 18:04
0

I needed this for a job and I did it in a very simple way I leave here for anyone who needs 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>
    
09.09.2015 / 16:14