Home > Mobile >  How to detect letters and theirs upper/lower case format from two characters which need to be compar
How to detect letters and theirs upper/lower case format from two characters which need to be compar

Time:04-29

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

CodePudding user response:

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', '?'));

CodePudding user response:

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', '?'));

CodePudding user response:

Presented below is one possible way to achieve the desired objective.

Code Snippet

// helper methods to determin lower, upper cases
const isLower = x => x === x.toLowerCase();
const isUpper = x => x === x.toUpperCase();

const sameCase = (a, b) => {

  // if either "a" or "b" are not "letter"s, return -1
  if (!a.match(/[a-z|A-Z]/) || !b.match(/[a-z|A-Z]/)) return -1;

  // if both "a" and "b" are either lower or upper case, return 1
  if ((isLower(a) && isLower(b)) || (isUpper(a) && isUpper(b))) return 1;
  
  // return 0 (since "a", "b" are letters, but not same case)
  return 0;
};

console.log("sameCase('a', 'b'): ", sameCase('a', 'b'));
console.log("sameCase('A', 'B'): ", sameCase('A', 'B'));
console.log("sameCase('a', 'B'): ", sameCase('a', 'B'));
console.log("sameCase('B', 'g'): ", sameCase('B', 'g'));
console.log("sameCase('0', '?'): ", sameCase('0', '?'));
.as-console-wrapper { max-height: 100% !important; top: 0 }

Explanation

Inline comments added to the snippet above.

CodePudding user response:

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

CodePudding user response:

Change your if condition and check upper/lower case.change 2nd number if condition return 0 to 1 (if (a.charAt(/A-Z/) && b.charAt(/A-Z/)) { return 0; }). and check non alphabetic value for -1. try below code.

function sameCase(a, b){
      if(a.match(/[a-z]/i) && b.match(/[a-z]/i)) {
         if (a.toLowerCase() === a && b.toLowerCase() === b) {
            return 1;
         }
         if (a.toUpperCase() === a && b.toUpperCase() === b) {
             return 1;
         }
          if (b.toLowerCase() === b && a.toUpperCase() === a || a.toLowerCase() === a && b.toUpperCase() === b) {
             return 0;
         }
      }else{
        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', '?'));
  • Related