1

I am trying to add a function to the global String object by doing the following:

function repetirCaracteres() {

  String.prototype.repeatCharacters = function(palabra){
  const letras = palabra.split('')
  const repetir = letras.map(letras => letras.repeat(2))
  let letrasRepetidas = repetir.join('');
  return letrasRepetidas;
  }
  
 }

repetirCaracteres()

I want to do a function that receives a string and each character of the string is repeated twice.

Logic seems ok to me but for some reason i have the following error:

String prototype is read only, properties should not be added.
tukitooo1998
  • 337
  • 10

1 Answers1

0

Don't know, why it did not work for you. It works here without problems:

String.prototype.rr=function(){
 return this.split("").map(c=>c+c).join("")}
console.log("ABC".rr())

In my snippet I used this as the input string. Without this there would be no reason for defining a String.prototype method.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43