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);
}
const str1 = "689454545454";
isOnlyDigitsUsingRegex(str1); //output: true
const str2 = "he is 24 years old";
isOnlyDigitsUsingRegex(str2); //output: false
function isOnlyDigitsUsingEvery(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return [...str].every(char => char >= '0' && char <= '9');
}
const str1 = "689454545454";
isOnlyDigitsUsingEvery(str1); //output: true
const str2 = "he is 24 years old";
isOnlyDigitsUsingEvery(str2); //output: false
function isOnlyDigitsUsingSome(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return ![...str].some(char => char < '0' || char > '9');
}
const str1 = "689454545454";
isOnlyDigitsUsingSome(str1); //output: true
const str2 = "he is 24 years old";
isOnlyDigitsUsingSome(str2); //output: false
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;
}
const str1 = "689454545454";
isOnlyDigitsUsingFilter(str1); //output: true
const str2 = "he is 24 years old";
isOnlyDigitsUsingFilter(str2); //output: false
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;
}
const str1 = "689454545454";
isOnlyDigitsUsingForLoop(str1); //output: true
const str2 = "he is 24 years old";
isOnlyDigitsUsingForLoop(str2); //output: false
function isOnlyDigitsUsingMatch(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return str.match(/^\d+$/) !== null;
}
const str1 = "689454545454";
isOnlyDigitsUsingMatch(str1); //output: true
const str2 = "he is 24 years old";
isOnlyDigitsUsingMatch(str2); //output: false
function isOnlyDigitsUsingReplace(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return str.replace(/^\d+$/, '') === '';
}
const str1 = "689454545454";
isOnlyDigitsUsingReplace(str1); //output: true
const str2 = "he is 24 years old";
isOnlyDigitsUsingReplace(str2); //output: false
function isOnlyDigitsUsingNumberConversion(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return !isNaN(Number(str)) && Number(str).toString() === str;
}
const str1 = "689454545454";
isOnlyDigitsUsingNumberConversion(str1); //output: true
const str2 = "he is 24 years old";
isOnlyDigitsUsingNumberConversion(str2); //output: false
function isOnlyDigitsUsingParseInt(str) {
if (typeof str !== 'string') {
throw new Error("Your input is not a string");
}
return str === parseInt(str, 10).toString();
}
const str1 = "689454545454";
isOnlyDigitsUsingParseInt(str1); //output: true
const str2 = "he is 24 years old";
isOnlyDigitsUsingParseInt(str2); //output: false
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));
}
const str1 = "689454545454";
isOnlyDigitsUsingSet(str1); //output: true
const str2 = "he is 24 years old";
isOnlyDigitsUsingSet(str2); //output: false