我正在创建一个类,用于将整数转换为自然语言的句子。我已经进行了一些基本检查以确保给出的数字在-9999和9999之间。我觉得这在大多数情况下都有效。
但是,一旦程序到达“this.convertSentence” - 通过try / catch块和错误检查,我想知道现在最好的做法是将问题分解为需要运行的各种函数调用完成工作。
我正在计划用this.convertSentence做一些检查数字大小等...然后将数字发送到单独的函数来做更多工作并让它们传播一个句子返回。我不确定我是否想要在我的班级中使用变量,或者我是否应该传递一个变量来构建句子。我想知道这样的事情。
/**
* 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());
答案 0 :(得分:1)
我的代码中发现了一些错误:
getSentence
和convertSentence
都公开?只有getSentence
应公开。我会做的事情:
以下是代码:
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
}
}();
然后你可以像这样使用它:
var number = 1337;
alert(number.toWord("es"));
编辑:我写了一个简单的函数,它会做你想要的。不过它是英文的。我不懂西班牙语,所以你必须自己实现。这是演示:http://jsfiddle.net/XKYhx/2/
答案 1 :(得分:0)
我的想法是检查你将对句子有多少部分并构建一个数组以匹配子串。例如,无论如何英语(我不会说西班牙语!)
作为自然语言,你会说(减去)xxx千和xxx 因为你的号码的最大/最小值为~10000 /〜-10000, 在伪代码中:
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.
我能想到的最好的故障是:
希望有帮助..正在思考。