Home > Back-end >  How do I check if a string contains both lowercase and uppercase letters in JS?
How do I check if a string contains both lowercase and uppercase letters in JS?

Time:12-29

I want to write a function that checks if a string contains lowercase and uppercase letters. If both are present, it should return true, and if either are missing, it should return false.

function lowercaseUppercase (letterCase) {
  
  if (letterCase == letterCase.toUpperCase()) 
  {
  return true; 
} else {
  return false; 
};  
}

console.log(lowercaseUppercase("GGG"))

This will return true if I use uppercase letters, and false if I pass lowercase letters to this function. If I substitute letterCase.toUpperCase() for letterCase.toLowerCase()it works. I want the function to check both parameters are true.

Can I use the && operator to check both?

function lowercaseUppercase (letterCase) {
  
  if (letterCase == letterCase.toUpperCase() &&
      letterCase == letterCase.toLowerCase()) 
  {
  return true; 
} else {
  return false; 
};  
}

This does not work and returns false for uppercase, lowercase, and a combination of both.

If both are present, it want it to return true, and if either are missing, it should return false.

Can anyone point out where I've gone wrong? :)

CodePudding user response:

You could check with a regular expression and return true if both cases are true.

Methods:

function allCases(string) {
    const
        upper = /[A-Z]/.test(string),
        lower = /[a-z]/.test(string);

    return upper && lower;
}

console.log(allCases('abc'));
console.log(allCases('ABC'));
console.log(allCases('abC'));
console.log(allCases('123'));

A different approach by itrating the string.

function allCases(string) {
    let upper = false,
        lower = false;

    for (const character of string) {
        if (character.toUpperCase() === character.toLowerCase()) continue;
        upper ||= character === character.toUpperCase();
        lower ||= character === character.toLowerCase();
        if (upper && lower) return true;
    }

    return false;
}

console.log(allCases('abc'));
console.log(allCases('ABC'));
console.log(allCases('abC'));
console.log(allCases('123'));

CodePudding user response:

What are you doing is just converting string to upper and lower case. Here a one liner solution.

const verifyString = (str)=> (/[a-z]/.test(str)) && (/[A-Z]/.test(str))

CodePudding user response:

The toUpperCase and toLowerCase functions operate on the entire string at once.

If you try this out on a js terminal you will see that:

"Hello World!".toUpperCase() === "HELLO WORLD!"

Based on your question, I think what you want is to check character by character.

You can use Array.from to convert the string into an array an then check if any character meets a given condition using the Array.some method:

const input = "Hello World!";
const containsUpperCase = Array.from(input).some((c) => c === c.toUpperCase());

References:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

(Edit) I realize the arrow function syntax might not be very beginner friendly so here is an alternative:

function charIsUppercase(c) {
    return c === c.toUpperCase();
}

const input = "Hello World!";
const containsUpperCase = Array.from(input).some(charIsUppercase);

CodePudding user response:

letterCase == letterCase.toUpperCase() does not actually check that there are uppercase letters in the string, it checks that there are no lowercase letters in the string. Consequentially, your && expression checks that there are neither lowercase nor uppercase letters in the string.

So for checking that there are both lowercase and uppercase letters, you'd need to do

function lowercaseUppercase (letterCase) {
  return !(letterCase == letterCase.toUpperCase() || letterCase == letterCase.toLowerCase());  
}

or

function lowercaseUppercase (letterCase) {
  return letterCase != letterCase.toUpperCase()  // has lowercase letters
      && letterCase != letterCase.toLowerCase(); // has uppercase letters
}
  • Related