How do you replace the letters?

0

This code replaces the string passed to the function in this way:

  • Returns it in capital letters
  • Returns each letter replaced by the letter that follows it in the alphabet

But I do not understand how some parts are made, comenté the code to explain what I DO NOT UNDERSTAND .

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
    });
    
    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?

    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.
  
    return capitalized; //Devuelve la string transformada
}

console.log(LetterChanges("codeby"));
    
asked by Eduardo Sebastian 24.09.2017 в 21:40
source

1 answer

2

I copy 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
    });

As you described, replace the z or Z with a .

This way of replacing is repeated throughout the code. The String.prototype.replace () function allows you to pass a function to the replacement (a callback). This function receives as a parameter the match of the regex (which is assigned to the variable i , and expects the value by which to replace it as a value returned by the function ( "a" in this case).

  

Anyway, this is the same as:

znZ = str.replace( /z/gi, "a");
     

because using the /i modifier is already ignoring   uppercase / lowercase, and clearly it is not necessary to pass a function   that 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, increase all the letters. And it is also correct that it has already replaced the z , so that both the z and the a of the original text are going to be replaced by b ... I assume that it is an oversight of who programmed it.

To be taken into account, the result of the replace () function is discarded. Instead, the variable results is used within the function. That way, any character that is not in the a-z range is ignored.


    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 by each coincidence of /a|e|i|i|u/gi (that is, in this case, for each letter). And within the function, the variable i (passed as a parameter) receives that vowel. The function then returns that vowel in capital letters.

In regex, a | implies alternation, that is, acts as an OR . So the expression can be interpreted as: a or e or i or i (yes, it is probably a typo 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 bit more efficient, and easier to   write and read). It would be the same as:

capitalized = results.replace(/[aeiu]/g, function(match){
    return match.toUpperCase();
});
     

Also note that the /i modifier is unnecessary (no need   replace the uppercase vowels with the same letter).

     

Also, it could have been set as a IF within 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 do not think it's good to be calling a new replace () for each condition.


    return capitalized; //Devuelve la string transformada
}

And that's how the modified text returns ... For what? No idea. It is not that a function like this is going to offer any degree of encryption, much less ... It seems more like a game, altering the letters (with some other error in between).

    
answered by 25.09.2017 / 00:48
source