0

I am creating a class to convert an integer to a sentence in a natural language. I've got some basic checks going on to ensure that the number given is between -9999 and 9999. I feel like this works for the most part.

However, once the program reaches "this.convertSentence" - past the try/catch block and error checking, I'm wondering what the best practice is now to decompose the problem into the various function calls it will need to run through to get the job done.

What I'm planning on doing with this.convertSentence is doing some checking for number size, etc...and then sending the number off to separate functions to do more work and having them propagate a sentence to return. I'm not sure if I want a variable just within my class to work with or whether I should be passing a variable around for the sentence to build. Things like this I am wondering about.

/**
*   A class for converting an integer to a natrual language sentence in Spanish.
*   Accepts integers from -9999 to 9999
*
*/
function NumberToWord () {

    this.getSentence = function(number) {

        // Check for erroneous input. Accepts only -9999 thru 9999 integers
        try
        {
            if(number === parseInt(number) && number > -10000 && number < 10000) {
                return this.convertSentence(number);
            } 
            else {
                throw new Error("Argument is not an integer between -9999 and 9999");
            }
        }
        catch(e){
            console.log(e.name + " " + e.message);
        }



    };

    this.convertSentence = function(number) {
        return "This is where I'll start the logic for the sentence";
    };

}


var numberToWord = new NumberToWord();




// Tests
console.log(numberToWord.getSentence(9999));
console.log(numberToWord.getSentence(-9999));
console.log(numberToWord.getSentence(10000));
console.log(numberToWord.getSentence(-10000));
console.log(numberToWord.getSentence(0));
console.log(numberToWord.getSentence(1.1));
console.log(numberToWord.getSentence(-9999.1));
console.log(numberToWord.getSentence(10001));
console.log(numberToWord.getSentence(-10001));
console.log(numberToWord.getSentence(5.5));
console.log(numberToWord.getSentence());
freedomflyer
  • 2,431
  • 3
  • 26
  • 38

2 Answers2

1

There are a few things I found amiss in your code:

  1. You don't need a class. You simply want to convert a number to a sentence. Use a function.
  2. Why are both getSentence and convertSentence public? Only getSentence should be public.
  3. Since your class will (in all probability) only be instatiated once, use the singleton pattern.

Things I would do:

  1. Because you want to make your code modular, I would use the module pattern.
  2. You can delegate specific tasks to different functions, but keep them in a private namespace.

Here's the code:

Number.prototype.toWord = function () {
    return function (lang) {
        var number = this.valueOf();

        if (parseInt(number) === number) {
            if (number < 10000 && number > 10000) {
                switch (lang) {
                case "es":
                    return toSpanish(number);
                case "en":
                default:
                    return toEnglish(number);                        
                }
            } else throw new RangeError("Expected an integer between ±10000.");
        } else throw new TypeError("Expected an integer.");
    };

    function toSpanish(number) {
        // convert the number to Spanish
    }

    function toEnglish(number) {
        // convert the number to English
    }
}();

Then you can use it like this:

var number = 1337;
alert(number.toWord("es"));

Edit: I wrote a simple function which will do what you want. However it's in English. I don't know Spanish so you'll have to implement that yourself. Here's the demo: http://jsfiddle.net/XKYhx/2/

Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
0

My thinking would be to check how many parts you are going to have to the sentence and build an array to match with the substrings. for example, in English anyway (I don't speak Spanish!)

as natural language you would say (minus) xxx thousand and xxx since your number has a max / min of ~10000 / ~-10000, in pseudocode:

    var sign = ""
    var wholeparts = new Array()
    var mantissaparts = new Array()
      if number < 0, 
      sign = "minus"
      number = math.abs(number) // turn the number into a positive number now we have the      sign
    var whole = math.floor(number) //get whole number 
    var mantissa = number - whole //get the after decimal part if exists
    if whole > 1000
      wholeparts.push(math.floor(whole/1000)) //get the thousands part 
      wholeparts.push(whole - parts[0]*1000) // add the hundreds
    else
      parts.push(whole)

 if mantissa.length > 0
   do something similar for the mantissa to the mantissaparts array.

At this point you would have the sentence structure broken down then:

string sentance:
 foreach (var part in wholeparts)
 stringify and check each number, converting to human words depending on index, ie "seven" or "seventy", add each to the string. 
  if wholeparts.length > 1 : sentence = sentence + " thousand and"

then if you had a mantissa, sentence = sentence + "point" .. then and the mantissa as natural language.

Best breakdown I can think of would be:

  • method to convert a number (whole or mantissa) to an array,
  • method to convert the array to natural language, with a parameter saying if it is whole or mantissa for the different wording that would be used.
  • method that accepts a number in string form and returns the natural language equivalent

Hope that helps .. was thinking on the fly.

Matt J
  • 21
  • 3