Write a function that determines if a given string contains only uppercase letters. The function should return true
if the string is entirely uppercase and false
otherwise.
Input: str = "AMANPAREEK"
Output: true
Input: str = "Aman"
Output: false
function isAllUppercaseUsingRegex(str) {
return /^[A-Z]+$/.test(str);
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingRegex(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingRegex(str2); //output: false
function isAllUppercaseUsingToUpperCase(str) {
return str === str.toUpperCase();
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingToUpperCase(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingToUpperCase(str2); //output: false
function isAllUppercaseUsingEvery(str) {
return [...str].every(char => char >= 'A' && char <= 'Z');
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingEvery(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingEvery(str2); //output: false
function isAllUppercaseUsingForLoop(str) {
for (let i = 0; i < str.length; i++) {
if (str[i] < 'A' || str[i] > 'Z') {
return false;
}
}
return true;
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingForLoop(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingForLoop(str2); //output: false
function isAllUppercaseUsingMatch(str) {
return str.match(/^[A-Z]+$/) !== null;
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingMatch(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingMatch(str2); //output: false
function isAllUppercaseUsingSplitEvery(str) {
return str.split('').every(char => char >= 'A' && char <= 'Z');
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingSplitEvery(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingSplitEvery(str2); //output: false
function isAllUppercaseUsingReduce(str) {
return [...str].reduce((allUppercase, char) => allUppercase && (char >= 'A' && char <= 'Z'), true);
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingReduce(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingReduce(str2); //output: false
function isAllUppercaseUsingIncludes(str) {
return !/[a-z]/.test(str);
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingIncludes(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingIncludes(str2); //output: false
function isAllUppercaseUsingReplace(str) {
return str.replace(/[A-Z]/g, '') === '';
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingReplace(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingReplace(str2); //output: false
function isAllUppercaseUsingForOf(str) {
for (const char of str) {
if (char < 'A' || char > 'Z') {
return false;
}
}
return true;
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingForOf(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingForOf(str2); //output: false
function isAllUppercaseUsingASCII(str) {
for (let i = 0; i < str.length; i++) {
const charCode = str.charCodeAt(i);
if (charCode < 65 || charCode > 90) {
return false;
}
}
return true;
}
const str1 = "AMANPAREEK";
isAllUppercaseUsingASCII(str1); //output: true
const str2 = "Aman";
isAllUppercaseUsingASCII(str2); //output: false