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

199
Vistas
How to detect letters and theirs upper/lower case format from two characters which need to be compared to each other?

I need to get:

  • If either of the characters is not a letter, return -1
  • If both characters are the same case, return 1
  • If both characters are letters, but not the same case, return 0

Examples:

'a' and 'g' returns 1

'A' and 'C' returns 1

'b' and 'G' returns 0

'B' and 'g' returns 0

'0' and '?' returns -1

Now my code is uncorrect:

function sameCase(a, b) {

  if (a.match(/a-z/) && b.match(/a-z/)) {
    return 1;
  }
  if (a.match(/A-Z/) && b.match(/A-Z/)) {
    return 0;
  }
  if (b.match(/a-z/) && a.match(/A-Z/)) {
    return 0;
  }

  return -1;
}

console.log(sameCase('a', 'b'));
console.log(sameCase('A', 'B'));
console.log(sameCase('a', 'B'));
console.log(sameCase('B', 'g'));
console.log(sameCase('0', '?'));

Help, please..

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

0

You are using regex incorrectly. You should have used /[a-z]/ if you want to check that your character is a letter from a to z.

function sameCase(a, b){
  if (a.match(/[a-z]/) && b.match(/[a-z]/)) {
    return 1;
  }
  if (a.match(/[A-Z]/) && b.match(/[A-Z]/)) {
    return 1;
  }
  if (b.match(/[a-z]/) && a.match(/[A-Z]/)) {
    return 0;
  }
  if (a.match(/[a-z]/) && b.match(/[A-Z]/)) {
    return 0;
  }
  return -1;
}
console.log(sameCase('a', 'b'));
console.log(sameCase('A', 'B'));
console.log(sameCase('a', 'B'));
console.log(sameCase('B', 'g'));
console.log(sameCase('0', '?'));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You are using the wrong syntax for the regular expressions.

/a-z/ matches the string a-z. If you want to test for a range of characters, you need to wrap the range in [].

Also your return values didn't match the expectations you described.

function sameCase(a, b) {

  if (
    (a.match(/[a-z]/) && b.match(/[a-z]/)) ||
    (a.match(/[A-Z]/) && b.match(/[A-Z]/))
  ) {
    return 1;
  }
  if (
    (b.match(/[a-z]/) && a.match(/[A-Z]/)) ||
    (b.match(/[A-Z]/) && a.match(/[a-z]/))  
  ) {
    return 0;
  }

  return -1;
}

console.log(sameCase('a', 'b'));
console.log(sameCase('A', 'B'));
console.log(sameCase('a', 'B'));
console.log(sameCase('B', 'g'));
console.log(sameCase('0', '?'));

about 4 years ago · Juan Pablo Isaza Denunciar

0

+++ Just as additional information +++

Building on top of connexo's approach and turning it into a solution which does not just cover the basic latin upper- and lower-case letters, one could replace each character class with its unicode property escapes counterpart which in addition covers each a much wider range of characters ...

// based on basic latin letter character classes.
function sameCaseBasicLatin(a, b) {
  if (
    (a.match(/[a-z]/) && b.match(/[a-z]/)) ||
    (a.match(/[A-Z]/) && b.match(/[A-Z]/))
  ) {
    return 1;
  }
  if (
    (b.match(/[a-z]/) && a.match(/[A-Z]/)) ||
    (b.match(/[A-Z]/) && a.match(/[a-z]/))  
  ) {
    return 0;
  }
  return -1;
}

// ... ok ...
console.log('`sameCaseBasicLatin` invoked with basic latin letters');

console.log(sameCaseBasicLatin('a', 'b'));
console.log(sameCaseBasicLatin('A', 'B'));
console.log(sameCaseBasicLatin('a', 'B'));
console.log(sameCaseBasicLatin('B', 'g'));
console.log(sameCaseBasicLatin('0', '?'));

// ... but ... not ok.
console.log('`sameCaseBasicLatin` invoked with diacritic latin letters');

console.log(sameCaseBasicLatin('â', 'ê'));
console.log(sameCaseBasicLatin('Â', 'Ê'));
console.log(sameCaseBasicLatin('â', 'Î'));
console.log(sameCaseBasicLatin('Ô', 'ä'));
console.log(sameCaseBasicLatin('-', '#'));


// make use of unicode property escapes.
function sameCaseLetters(a, b) {

  // lowercase letter as unicode property escape.
  const regXLowerCaseLetter = (/\p{Ll}/u);
  // uppercase letter as unicode property escape.
  const regXUpperCaseLetter = (/\p{Lu}/u);

  if (
    (regXLowerCaseLetter.test(a) && regXLowerCaseLetter.test(b)) ||
    (regXUpperCaseLetter.test(a) && regXUpperCaseLetter.test(b))
  ) {
    return 1;
  }
  if (
    (regXLowerCaseLetter.test(a) && regXUpperCaseLetter.test(b)) ||
    (regXUpperCaseLetter.test(a) && regXLowerCaseLetter.test(b))  
  ) {
    return 0;
  }
  return -1;
}

// ... ok ...
console.log('`sameCaseLetters` invoked with basic latin letters');

console.log(sameCaseLetters('a', 'b'));
console.log(sameCaseLetters('A', 'B'));
console.log(sameCaseLetters('a', 'B'));
console.log(sameCaseLetters('B', 'g'));
console.log(sameCaseLetters('0', '?'));

// ... and ... also ok.
console.log('`sameCaseLetters` invoked with diacritic latin letters');

console.log(sameCaseLetters('â', 'ê'));
console.log(sameCaseLetters('Â', 'Ê'));
console.log(sameCaseLetters('â', 'Î'));
console.log(sameCaseLetters('Ô', 'ä'));
console.log(sameCaseLetters('-', '#'));
.as-console-wrapper { min-height: 100%!important; top: 0; }

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