Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!