最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I check if a string contains both lowercase and uppercase letters in JS? - Stack Overflow

programmeradmin5浏览0评论

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 bination 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? :)

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 bination 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? :)

Share Improve this question asked Dec 28, 2021 at 16:39 Joe Joe 354 silver badges9 bronze badges 2
  • if I understand you correctly, you want a function that only returns true when the input has mixed cases (i.e. if entirely lowercase or entirely uppercase, return false). in that case, you could replace AND operator with OR operator, and swap your return statements. – jun Commented Dec 28, 2021 at 16:48
  • This is exactly what I was after. Thank you! – Joe Commented Dec 28, 2021 at 17:17
Add a ment  | 

4 Answers 4

Reset to default 4

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

Methods:

  • RegExp#test
  • logical AND &&

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

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/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

https://developer.mozilla/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);

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

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
}
发布评论

评论列表(0)

  1. 暂无评论