Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
Why doesn't this function change the case of the string when it has a similar character like the first letter?

Can't understand why the following function works for some strings an didn't work for some which has a similar character like the first.

const change = (str,y) => (str.toLowerCase().replace(str[y],str[y].toUpperCase()));

console.log(change('London',3)); //lonDon
console.log(change('Lagos',3)); //lagOs
console.log(change('Germany',3)); //gerMany
console.log(change('Dcoder',3)); //Dcoder 
console.log(change('Bobby',3)); //Bobby

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The function String.prototype.replace replaces the first occurrence when the first argument is a string.

You can split the string change the index to uppercase and finally join the chars.

const change = (str, y) => {
  const lowercase = str.toLowerCase().split("");
  lowercase[y] = str[y].toUpperCase();
  return lowercase.join("");
};

console.log(change('London',3));
console.log(change('Lagos',3));
console.log(change('Germany',3));
console.log(change('Dcoder',3));
console.log(change('Bobby',3));

about 4 years ago · Juan Pablo Isaza Denunciar

0

str[3] in "Dcoder" is 'd' and in replace the first charecter is 'D' , so than replaced you should change code to this:

const change = (str,y) => {
str = str.toLowerCase()           
str=str.slice(0,y)+str[y].toUpperCase()+str.slice(y+1)    
return str}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Or, in a slightly shortened manner:

const change = (str, n) =>
  str.split("")
     .map((c,i)=>i==n?c.toUpperCase():c.toLowerCase())
     .join("");

['London','Lagos','Germany','Dcoder','Bobby'].forEach(w=>console.log(change(w,3)));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda