Write a function that checks whether a given string consists exclusively of digit characters (i.e., 0 through 9). The function should return true if the string contains only digits, and false
Input: str = "689454545454"
Output: true
Input: str = "he is 24 years old"
Output: false
function isOnlyDigitsUsingRegex(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
const re = /^\d+$/;
return re.test(str);
}function isOnlyDigitsUsingEvery(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return [...str].every(char => char >= '0' && char <= '9');
}function isOnlyDigitsUsingSome(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return ![...str].some(char => char < '0' || char > '9');
}function isOnlyDigitsUsingFilter(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return str.split("").filter(char => char < '0' || char > '9').length === 0;
}function isOnlyDigitsUsingForLoop(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
for (let i = 0; i < str.length; i++) {
if (str[i] < '0' || str[i] > '9') {
return false;
}
}
return true;
}function isOnlyDigitsUsingMatch(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return str.match(/^\d+$/) !== null;
}function isOnlyDigitsUsingReplace(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return str.replace(/^\d+$/, '') === '';
}function isOnlyDigitsUsingNumberConversion(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return !isNaN(Number(str)) && Number(str).toString() === str;
}function isOnlyDigitsUsingParseInt(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return str === parseInt(str, 10).toString();
}function isOnlyDigitsUsingSet(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
const digits = new Set('0123456789');
return [...str].every(char => digits.has(char));
}