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

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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