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

442
Vistas
Code wars: Why does this function work against whitespace and numbers?

I need to check if two strings are in the same case. I have my own solution to this problem but I found this version and I can't understand how/why it works:

function sameCase(a, b){
 if(a.toUpperCase() === a.toLowerCase() || b.toLowerCase() === b.toUpperCase()){
    return -1
  }else if(a === a.toLowerCase() && b === b.toLowerCase() || a === a.toUpperCase() && b === b.toUpperCase()){
      return 1
  }else{
    return 0
  }
}

Specifically this if statement:

if(a.toUpperCase() === a.toLowerCase() || b.toLowerCase() === b.toUpperCase()){ return -1}

I know this check works against numeric characters and white space, but I don't understand why this works since it doesn't look to take these items into account.

my question: why does this function work against non-letters and numbers and even white space?

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

0

The non-letters are not modified by toUpperCase or toLowerCase. Try picturing a lowercase 9 or . That's because all those functions do is replace the individual letter characters with their uppercase or lowercase equivalents.

So, that code segment just basically checks if the string is entirely composed of non-letter characters (because 924 " is the same in both uppercase or lowercase, however a123 is not the same in both uppercase and lowercase and thus will fail that if statement).

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