Tuesday, September 9, 2014

Display a natural number as text

This is an interview question I was given a while ago that I thought was an interesting challenge.  Personally I felt it was a little too long of a question to work well in an interview but it was a nice little puzzle to figure out.  The solution provided is just to solve the problem using core functionality with no effort made to be elegant in the solution. You will note in my solution that I do not provide a text value for anything over a billion because it just seemed purely academic to go beyond.

The problem:

Display any natural number as text?

example:
       input: 123
       output: one hundred twenty three


The source code and an interactive demo is available at http://jsfiddle.net/jtSdv/

Here is the javascript code itself for the link above:

var textValues = {};

function intAsText(value) {
    var result = "";
    if (isNaN(value)) {
        result = "n/a";
    } else {
        var intValue = parseInt(value);
        result = getText(value);
    }
    $("#output").text(result);
}

function getText(intValue) {
    var result = "";
    if (20 >= intValue) {
        result = textValues[intValue];
    } else if (1000 > intValue) {
        var firstDigit = intValue % 10
        var tensDigit = (intValue - firstDigit) % 100;
        var hundredsDigit = (intValue - tensDigit - firstDigit) / 100;
        if (hundredsDigit != 0) {
            result = textValues[hundredsDigit] + " " + textValues[100];
        }
        if (tensDigit != 0) {
            result = result + " " + textValues[tensDigit];
        }
        if (firstDigit != 0) {
            result = result + " " + textValues[firstDigit];
        }
    } else {
        var chunks = {}
        var workingValue = intValue;
        var multiple = 1000;
        var numChunks = 0;
        while (workingValue >= 1) {
            var chunk = workingValue % multiple
            chunks[numChunks] = chunk;
            workingValue = (workingValue - chunk) / multiple;
            numChunks++;
        }
        if (chunks[0] > 0) {
            result = getText(chunks[0]);
        }
        for (var i = 1; i < numChunks; i++) {
            if (chunks[i] > 0) {
                result = getText(chunks[i]) + " " + 
                    textValues[Math.pow(multiple, i)] + " " + result;
            }
        }
    }
    return result;
}


$(document).ready(function () {
    textValues[0] = "zero";
    textValues[1] = "one";
    textValues[2] = "two";
    textValues[3] = "three";
    textValues[4] = "four";
    textValues[5] = "five";
    textValues[6] = "six";
    textValues[7] = "seven";
    textValues[8] = "eight";
    textValues[9] = "nine";
    textValues[10] = "ten";
    textValues[11] = "eleven";
    textValues[12] = "twelve";
    textValues[13] = "thirteen";
    textValues[14] = "fourteen";
    textValues[15] = "fifteen";
    textValues[16] = "sixteen";
    textValues[17] = "seventeen";
    textValues[18] = "eighteen";
    textValues[19] = "nineteen";
    textValues[20] = "twenty";
    textValues[30] = "thirty";
    textValues[40] = "forty";
    textValues[50] = "fifty";
    textValues[60] = "sixty";
    textValues[70] = "seventy";
    textValues[80] = "eighty";
    textValues[90] = "ninety";
    textValues[100] = "hundred";
    textValues[1000] = "thousand";
    textValues[1000000] = "million";
});


Other Coding Challenges:




No comments:

Post a Comment