JavaScript - Checking For Any Lowercase Letters In A String
Consider a JavaScript method that needs to check whether a given string is in all uppercase letters. The input strings are people's names. The current algorithm is to check for an
Solution 1:
function hasLowerCase(str) {
return str.toUpperCase() != str;
}
console.log("HeLLO: ", hasLowerCase("HeLLO"));
console.log("HELLO: ", hasLowerCase("HELLO"));
Solution 2:
also:
function hasLowerCase(str) {
return (/[a-z]/.test(str));
}
Solution 3:
function hasLowerCase(str) {
return str.toUpperCase() != str;
}
or
function hasLowerCase(str) {
for(x=0;x<str.length;x++)
if(str.charAt(x) >= 'a' && str.charAt(x) <= 'z')
return true;
return false;
}
Solution 4:
Another solution only match regex to a-z
function nameHere(str) {
return str.match(/[a-z]/);
}
or
function nameHere(str) {
return /[a-z]/g.test(str);
}
Post a Comment for "JavaScript - Checking For Any Lowercase Letters In A String"