Exchanging last letter of a word using the 'replace() method

Asked

Viewed 180 times

2

I’m doing an exercise that asks me to turn the word Roberto in Roberta using the method replace(), and by my logic this solution should work:

var roberto = 'Roberto';

roberto.replace( roberto.charAt(6), 'a' );
// Raberto

Using the charAt() and passing the letter position by parameter works smoothly with all other letters of the word, but when I step to position 6, that would be just the letter I need (the last in the case) it takes the second letter instead of the last. Would anyone know to tell me why this occurs?

OBS.: The solution HAS than using the method replace().

  • 1

    Just one observation: charAt(6) is an object, so you would have to convert it to string.

  • 1

    It would be something like this: var roberto = 'Roberto', op = (roberto.charAt(6)).toString();
roberto.replace(op, 'a'); and yet you would still be: Raberto, because simple replace is only executed in the first character found.

  • 1

    It would be the same as roberto.replace('o', 'a'); to work, you would have to do so: var roberto = 'Roberto', op = new RegExp((roberto.charAt(6)).toString()+"$", 'gim'); roberto.replace(op, 'a');

2 answers

5


This is happening because the letter the is repeated twice. One at position 2 and the other at position 6. This way replace the first occurrence only, not the second or third.

You will not succeed using pure texts, as shown in your example. You may have this same problem several times and it is not interesting to go rogue to get the last occurrence only.

Alternatively, using the replace, you can use regular expression for this, looking for any character at the end of the string.

var roberto = 'Roberto';

roberto = roberto.replace(/.$/, "a");

console.log(roberto);

  • It worked perfectly, thanks @Cypherpotato!

  • @Augustovasques pardon, I did not understand the question ... ?

  • @Augustovasques ah yes, no problem .. rs'

4

If you have to use this function the only way is to use a Regex to determine a pattern that does what you want.

You can’t pass the position and you can’t just pass which character will be changed because it will do so in the first one you find.

Then you need to find a regular expression pattern that finds the last character to swap, in case the $ does it for you.

var roberto = 'Roberto';
console.log(roberto.replace(/o$/, 'a'));

I put in the Github for future reference.

Has documentation to study everything you can do with Regex. It’s very powerful and every pattern you want can be found, even if it’s a little complex to create something like this.

I prefer to do this in a more performative way without using the replace(), precisely for this reason and for being less readable, but there is the solution according to the requirement.

  • You were faster.

  • Faster q some, faster q others...

  • 1

    What I thought was funny the same person negatively negatively to mine and positively to another that is the same thing.

  • Of the will to restore my answer just to see if one would do.

  • @Augustovasquesques who has negatively got issues with me, not with you. And she’s using old-school bullshit to get away with it, because if I punish her, I could tell her it’s me stalking her. But she’s digging a bigger and bigger hole, where it’s hard to say she’s not chasing.

  • So if I was positive about mine, the cyber guy, and the guy who was negative about yours, that would be a lot of contradiction in a vote to show it. All three are the same.

  • 1

    @Augustovasques is what Maniero said: ancient bullshit.

  • Yes, the one I gave negative here (different from the one I gave today in another of JS) was because I gave a negative in his reply after I commented that I was wrong and he did not want to correct, I showed with several sources the error and even then he insisted that he was right. It is for these things that it is better not to comment, if I had only negatively he would continue positively praising me publicly as he did before. Some people are not "old" to participate in community.

Show 3 more comments

Browser other questions tagged

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