Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
How to add toAlternatingCase custom method to String class in JavaScript?

I'm trying to add a custom method to the String class in JavaScript called toAlternatingCase, it will turn lowercase characters into uppercase and vice versa in the string that is called on, I'm trying to make it like the built-in toUpperCase/toLowerCase methods which don't take any arguments. this is a kata(challenge) on codewars.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This can be done by utilizing the prototype method to extend the String class. This probably isn't the most efficient way of doing this, but it can be achieved with something like this:

String.prototype.toAlternatingCase = function() {
  var ns = "";
  for(var i = 0; i < this.length; i++) ns += (this.slice(i, i+1) == this.slice(i, i+1).toUpperCase()) ? this.slice(i, i+1).toLowerCase() : this.slice(i, i+1).toUpperCase();
  return ns;
}

"String.prototype.toAlternatingCase".toAlternatingCase();
// Should return 'sTRING.PROTOTYPE.TOaLTERNATINGcASE'

This just loops through the string and reverses the case of each character, then returns that new result (without modifying the original string).

about 4 years ago · Juan Pablo Isaza Relatório

0

String.prototype.toAlternatingCase = function () {
  return this.split("").map(l => l === l.toUpperCase() ? l.toLowerCase() : l.toUpperCase()).join("");
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda