DEV Community

Cover image for Top 10 String Javascript Interview Coding Question
Vaibhav Shukla
Vaibhav Shukla

Posted on

Top 10 String Javascript Interview Coding Question

1. Reverse a String

function reverseString(str) {
    return str.split('').reverse().join('');
}
console.log(reverseString("hello")); // Output: "olleh"

Enter fullscreen mode Exit fullscreen mode

2. How to check the name of the number in English?

function numberToWords(number) {
    const singleDigits = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
    const teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
    const tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

    if (number === 0) {
        return 'zero';
    }

    if (number < 10) {
        return singleDigits[number];
    }

    if (number < 20) {
        return teens[number - 10];
    }

    if (number < 100) {
        return tens[Math.floor(number / 10)] + ' ' + singleDigits[number % 10];
    }

    if (number < 1000) {
        return singleDigits[Math.floor(number / 100)] + ' hundred ' + numberToWords(number % 100);
    }

    if (number < 1000000) {
        return numberToWords(Math.floor(number / 1000)) + ' thousand ' + numberToWords(number % 1000);
    }

    if (number < 1000000000) {
        return numberToWords(Math.floor(number / 1000000)) + ' million ' + numberToWords(number % 1000000);
    }

    return 'Number too large to convert';
}

// Example usage:
console.log(numberToWords(123)); // Outputs: ""one hundred twenty three""
console.log(numberToWords(12345)); // Outputs: ""twelve thousand three hundred forty five""
console.log(numberToWords(123456789)); // Outputs: ""one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine"""

Enter fullscreen mode Exit fullscreen mode

*3. How to calculate the number of vowels and consonants in a string?
*

function countVowelsAndConsonants(str: string): { vowels: number, consonants: number } {
    // Define a regular expression to match vowels (case-insensitive)
    const vowelRegex = /[aeiou]/i;

    let vowels = 0;
    let consonants = 0;

    // Convert the string to lowercase for case-insensitive comparison
    const lowerCaseStr = str.toLowerCase();

    // Iterate through each character of the string
    for (let char of lowerCaseStr) {
        // Check if the character is a letter
        if (/[a-z]/i.test(char)) {
            // Increment the count of vowels if the character is a vowel
            if (vowelRegex.test(char)) {
                vowels++;
            } else {
                // Increment the count of consonants if the character is a consonant
                consonants++;
            }
        }
    }

    // Return the count of vowels and consonants
    return { vowels, consonants };
}

// Example usage:
const inputString = ""Hello World"";
const { vowels, consonants } = countVowelsAndConsonants(inputString);
console.log(""Vowels:"", vowels); // Output: 3
console.log(""Consonants:"", consonants); // Output: 7
Enter fullscreen mode Exit fullscreen mode

4. how to calculate 2 largest vowel count from string

function calculateTwoLargestVowelCounts(str: string): number[] {
    // Define an array to store counts of each vowel (a, e, i, o, u)
    const vowelCounts: number[] = [0, 0, 0, 0, 0];

    // Convert the string to lowercase for case-insensitive comparison
    const lowerCaseStr = str.toLowerCase();

    // Iterate through each character of the string
    for (let char of lowerCaseStr) {
        // Check if the character is a vowel
        switch (char) {
            case 'a':
                vowelCounts[0]++;
                break;
            case 'e':
                vowelCounts[1]++;
                break;
            case 'i':
                vowelCounts[2]++;
                break;
            case 'o':
                vowelCounts[3]++;
                break;
            case 'u':
                vowelCounts[4]++;
                break;
        }
    }

    // Sort the array in descending order to find the two largest counts
    const sortedCounts = vowelCounts.slice().sort((a, b) => b - a);

    // Return the two largest counts
    return sortedCounts.slice(0, 2);
}

// Example usage:
const inputString = ""Hello World"";
const twoLargestCounts = calculateTwoLargestVowelCounts(inputString);
console.log(""Two Largest Vowel Counts:"", twoLargestCounts); // Output: [2, 1]

Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs