I need to get:
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..
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', '?'));
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', '?'));
+++ 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; }