0

:) 我有这个练习,我必须将 < 1000 的数字转换成英文单词。我正在尝试使用开关,但它无法返回数百 + 十年 + 单曲,因此它仅适用于 10 到 20 之间的数字。这是我的代码,我在在线 lint 检查器中检查了它,没有发现任何语法错误。

function check() {
var theinput = document.getElementById("theinput");
var answer = document.getElementById("answer");
var number = parseInt(theinput.value);
var singles = number % 10;
var decades = (number / 10) % 10;
var hundreds = (number / 100) % 10;
var text = "The english word for the given number is: ";

if (isNaN(number)) {
    result = "Invalid Integer!";
}
    else {
        if (number > 999) {

            result = "Please enter a maximum 3 digit number!"
        }
            else {
                if (10 < number < 20) {
                    var teens = number;
                    switch (teens) {

                        case 11: result = text + "Eleven"; break;
                        case 12: result = text + "Twelve"; break;
                        case 13: result = text + "Thrirteen"; break;
                        case 14: result = text + "Fourteen"; break;
                        case 15: result = text + "Fifteen"; break;
                        case 16: result = text + "Sixteen"; break;
                        case 17: result = text + "Seventeen"; break;
                        case 18: result = text + "Eighteen"; break;
                        case 19: result = text + "Nineteen"; break;   

                   }
                }
                    else {
                        switch (singles) {

                            case 1: singles = "One"; break;
                            case 2: singles = "Two"; break;
                            case 3: singles = "Three"; break;
                            case 4: singles = "Four"; break;
                            case 5: singles = "Five"; break;
                            case 6: singles = "Six"; break;
                            case 7: singles = "Seven"; break;
                            case 8: singles = "Eight"; break;
                            case 9: singles = "Nine"; break;
                            default: singles = " ";

                        }
                        switch (decades) {

                            case 2: decades = "Twenty"; break;
                            case 3: decades = "Thrirty"; break;
                            case 4: decades = "Fourty"; break;
                            case 5: decades = "Fifty"; break;
                            case 6: decades = "Sixty"; break;
                            case 7: decades = "Seventy"; break;
                            case 8: decades = "Eightty"; break;
                            case 9: decades = "Ninety"; break;
                            default: decades = " ";  

                        }
                        switch (hundreds) {

                            case 1: hundreds = "One hundred and "; break;
                            case 2: hundreds = "Two hundred and "; break;
                            case 3: hundreds = "Three hundred and "; break;
                            case 4: hundreds = "Four hundred and "; break;
                            case 5: hundreds = "Five hundred and "; break;
                            case 6: hundreds = "Six hundred and "; break;
                            case 7: hundreds = "Seven hundred and "; break;
                            case 8: hundreds = "Eight hundred and "; break;
                            case 9: hundreds = "Nine hundred and "; break;
                            default: hundreds = "";
                        }

                        result = text + hundreds + decades + singles;
                    }
            }
    }

    answer.innerHTML = result;        
}
4

1 回答 1

0

您的 10 到 20 之间的代码

if (10 < number < 20) {

是错的

尝试

if (number > 10 && number < 20) {
于 2013-11-14T08:26:04.873 回答