JavaScript Program to Test if Kth Character is Digit in String

Testing if the Kth character in a string is a digit in JavaScript involves checking the character at the specified index and determining if it is a numeric digit.

Examples:

Input : test_str = ‘Beginner9Beginner’, K = 5 
Output : True
Explanation : 5th idx element is 9, a digit, hence True.
Input : test_str = ‘Beginner9Beginner’, K = 4
Output : False
Explanation : 4th idx element is s, not a digit, hence False.

These are the following approaches by using these we can check whether the digit is present at the Kth index or not:

Table of Content

  • Using charAt():
  • Using regular expression:
  • Using loop
  • Using codePointAt
  • Using Character Comparison
  • Using Array some Method

Using charAt():

In this approach, we check if the character at the Kth index in a string is a digit. It uses the ‘charAt’ method to retrieve the character at the specified index and the ‘includes’ method to check if the character exists in the numeric string “0123456789”. The result is a boolean indicating whether the Kth element is a digit in the string.

Example: This example shows the use of the above-explained approach.

Javascript
// Initializing string
let test_str = 'BeginnerBeginner';

// Printing original string
console.log("The original string is : " + test_str);

// initializing K
let K = 5;

// Checking if Kth character is a
// digit in the string

// Getting numeric string
let num_str = "0123456789";
let res = num_str.includes(test_str.charAt(K));

// Printing result
console.log("Is Kth element String : " + res);

Output
The original string is : BeginnerBeginner
Is Kth element String : false

Using regular expression:

In this approach, we are using the ‘match’ method to check if the character at the Kthj index in a string matches the regular expression pattern for a single digit `\d`. It stores the result in a boolean variable ‘res’ using the double negation ‘!!’ to convert the match result to a boolean value. Finally, it prints whether the Kth element is a digit in the string.

Example: This example shows the use of the above-explained approach.

Javascript
// Initialize string
let test_str = 'Beginner4Beginner';

// Print original string
console.log("The original string is : " + test_str);

// Initialize K
let K = 5;

// Regular expression pattern for a single digit
let pattern = /\d/;

// Check if the Kth character
// matches the pattern
let match = test_str.charAt(K).match(pattern);

// Set the value of res based on the match result
let res = !!match;

// Print result
console.log("Is Kth element String : " + res);

Output
The original string is : Beginner4Beginner
Is Kth element String : true

Using loop

This approach iterates over each character in the string and checks if the current index matches the kth position. If it does, it checks if the character is a digit. If the character is a digit it returns true otherwise, it returns false.

Example: This example shows the use of the above-explained approach.

JavaScript
function isKthCharacterDigit(str, k) {
    for (let i = 0; i < str.length; i++) {
        if (i === k - 1) {
            return /\d/.test(str[i]);
        }
    }
    // Return false if k is out of bounds
    
    return false; 
}

let string = "Beginner57f2orBeginner";
let k = 5;
console.log(`Is the ${k}th character a digit: ${isKthCharacterDigit(string, k)}`);

Output
Is the 5th character a digit: false

Using codePointAt

In this approach we use the codePointAt method to get the Unicode code point of the character at the Kth position and check if it falls within the Unicode range of digits.

Example: This example shows the use of the above-explained approach.

JavaScript
function isKthCharacterDigit(str, k) {
    const code = str.codePointAt(k);
    return code >= 48 && code <= 57; 
}

const string = "Hello123World";
const k = 5;
console.log(`Is the ${k}th character a digit: ${isKthCharacterDigit(string, k)}`);

Output
Is the 5th character a digit: true

Using Character Comparison

In the Character Comparison approach, we determine if the Kth character of a string is a digit by comparing it to character codes or ranges. Specifically, we check if the character falls between ‘0’ and ‘9’.

Example: This example shows the use of the above-explained approach.

JavaScript
function isKthCharDigit(str, k) {
    if (k < 0 || k >= str.length) {
        return false;
    }
    const char = str[k];
    return char >= '0' && char <= '9';
}

console.log(isKthCharDigit("Beginner4Beginner5", 5));

Output
true

Using Array some Method

In this approach, we utilize the some method of arrays. We split the string into an array of characters and check if the character at the Kth index is a digit. The some method checks if any character at the Kth position is a digit using a helper function.

Example: This example shows the use of the above-explained approach.

JavaScript
// Function to check if Kth character is a digit using Array some method
function isKthCharDigit(str, k) {
    const numStr = "0123456789".split('');
    return numStr.some((digit) => digit === str.charAt(k));
}

// Test the function
const testStr = "Beginner3Beginner";
const k = 5;
console.log(`Is the ${k}th character a digit: ${isKthCharDigit(testStr, k)}`);

// Output
// Is the 5th character a digit: true

Output
Is the 5th character a digit: true