w3resource

JavaScript: Conversion of integers to English words

JavaScript Math: Exercise-105 with Solution

Write a JavaScript program that converts a non-negative integer number to its English representation.

Test Data:
(1002) -> One Thousand Two.
(1279) -> One Thousand Two Hundred Seventy Nine.
(127900) -> One Hundred Twenty Seven Thousand Nine Hundred.
(1279000) -> One Million Two Hundred Seventy Nine Thousand.

Sample Solution:

JavaScript Code:

/**
 * Function to convert a given number into words.
 * @param {number} n - The number to be converted into words.
 * @returns {string} - The word representation of the given number.
 */
function test(n) {
    if (n < 0)
        return false;
    
    // Arrays to hold words for single-digit, double-digit, and below-hundred numbers
    single_digit = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']
    double_digit = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']
    below_hundred = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
    
    if (n === 0) return 'Zero';
    
    // Recursive function to translate the number into words
    function translate(n) {
        let word = "";
        if (n < 10) {
            word = single_digit[n] + ' ';
        } else if (n < 20) {
            word = double_digit[n - 10] + ' ';
        } else if (n < 100) {
            let rem = translate(n % 10);
            word = below_hundred[(n - n % 10) / 10 - 2] + ' ' + rem;
        } else if (n < 1000) {
            word = single_digit[Math.trunc(n / 100)] + ' Hundred ' + translate(n % 100);
        } else if (n < 1000000) {
            word = translate(parseInt(n / 1000)).trim() + ' Thousand ' + translate(n % 1000);
        } else if (n < 1000000000) {
            word = translate(parseInt(n / 1000000)).trim() + ' Million ' + translate(n % 1000000);
        } else {
            word = translate(parseInt(n / 1000000000)).trim() + ' Billion ' + translate(n % 1000000000);
        }
        return word;
    }
    
    // Get the result by translating the given number
    let result = translate(n);
    return result.trim() + '.';
}

n = 1002;
console.log("Number n = " + n);
console.log("In word: " + test(n));
n = 1279;
console.log("Number n = " + n);
console.log("In word: " + test(n));
n = 127900;
console.log("Number n = " + n);
console.log("In word: " + test(n));
n = 1279000;
console.log("Number n = " + n);
console.log("In word: " + test(n));

Output:

Number n = 1002
In word: One Thousand Two.
Number n = 1279
In word: One Thousand Two Hundred Seventy Nine.
Number n = 127900
In word: One Hundred Twenty Seven Thousand Nine Hundred.
Number n = 1279000
In word: One Million Two Hundred Seventy Nine Thousand.

Flowchart:

JavaScript: Conversion of integers to English words.

Live Demo:

See the Pen javascript-math-exercise-105 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.

Previous: Distinct ways to climb the staircase.
Next: Least number of perfect square that sums upto n.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/javascript-exercises/javascript-math-exercise-105.php