I collect the fragments of your code and I'm describing:function LetterChanges(str) {
var results = "", vowels = "aeiou", capitalized = "", char = "", znZ = "";
znZ = str.replace(/z|Z/gi, function(i){
return "a"; // Reemplaza la z o Z por a
});
Just as you described, replace. z or Z for a.This way of replacing is repeated throughout the code. Function https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/String/replace allows to pass a replacement function (a callback). That function receives as a parameter the regex match (which is assigned to the variable iand expects the value by which to replace it as value returned by the function ("a" in this case).Anyway, this is the same as:znZ = str.replace( /z/gi, "a");
because by using the modifier /i He's already ignoring
capital/minuscule, and clearly it is not necessary to pass a function
always return the same result. znZ.replace(/[a-z]/gi, function(i){
return results += String.fromCharCode(i.charCodeAt() + 1);
}); // Ahora si encuentra una letra entre a-z('i' - > insensitivo) la reemplazara por su proximo charCode(lo hace sumandole 1), pero esto no debería funcionar porque ya reemplazo la z en la anterior funcion, no?
It's correct, it increases all the letters. And it's also right that you've replaced the z, so the z and a of the original text will be replaced by b... I assume it's a carelessness of those who programmed it.To take into account, the result of the replace() function is discarded. Instead, the variable is used results within the function. That way, you're ignoring any character that's not in the a-z. capitalized = results.replace(/a|e|i|i|u/gi, function(i){
return i.toUpperCase();
}); // Sé que aqui las quiere colocar en mayusculas pero no entiendo como lo hace, mas bien no entiendo como funciona la expresión regular.
Again, using a callback, so the function is called every time for each match /a|e|i|i|u/gi (i.e., in this case, for every letter). And within the function, the variable i (passed as a parameter) receives that vowel. The function then returns that vowel in uppercases.In regex, one | implies alternation, so it acts as a OR. So the expression can be interpreted as: a or e or i or i (yes, it's probably a typing error of the programmer, so this second "i" is unnecessary or should be replaced by "o") or u.Now, to match a letter, you should use a class
of characters (which is a little more efficient, and easier
writing and reading). It would be the same as:capitalized = results.replace(/[aeiu]/g, function(match){
return match.toUpperCase();
});
Note also that the modifier /i is unnecessary (no need
replace the vowels in the capital with the same letter).Besides, he could've been like a IF inside the previous replacement (with a-z).
If I was already replacing all the letters, it was only necessary to see if it was a vowel there and return it in capital letters.
I don't think it's good to be calling a new replace() for each condition. return capitalized; //Devuelve la string transformada
}
And so return the modified text... For what? No idea. Not that a function like this is going to offer some degree of encryption, much less... It seems more like a game, altering the letters (with some other error by means).