如何让我的代码在JavaScript中将数字转换为单词?

pkwftd7m  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(127)

因此,我代码中的HTML只是创建一个基本游戏的一部分,该游戏生成一个随机数字并将其转换为一个单词,因此用户必须键入数字的数字形式。
有人能帮助我,因为我似乎不能让我的头周围如何使转换工作或建议一个更好的方法?

function btnStart_onClick() {
    lblrdm.innertext = parseInt(Math.random() * 999);
    parQuest.innerText = "What is this number in Digits? " + Convert(lblrdm.innertext);
    btnStart.disabled = true
    btnCheck.disabled = false
}

function Convert(num) { // THIS IS WHERE IM STUCK
    var ones = new Array('', ' one', ' two', ' three', ' four', ' five', ' six', ' seven', ' eight', ' nine', ' ten', ' eleven', ' twelve', ' thirteen', ' fourteen', ' fifteen', ' sixteen', ' seventeen', ' eighteen', ' nineteen');
    var tens = new Array('', '', ' twenty', ' thirty', ' forty', ' fifty', ' sixty', ' seventy', ' eighty', ' ninety');
    var hundred = ' hundred';
    var output = '';
    var numString = "num.toString()";

    if (num < 20) {
        output = ones[num];
        return output;
    }

    if (numString.length == 3) {
        output = ones[parseInt(numString.charAt(0))] + hundred;
        output += tens[parseInt(numString.charAt(1))];
        output += ones[parseInt(numString.charAt(2))];
        return output;
    }

    output += tens[parseInt(numString.charAt(0))];
    output += ones[parseInt(numString.charAt(1))];

    return output;
}
<p id="parQuest"></p><!-- basic html -->
<input id="btnStart" type="button" value="Start" onclick="btnStart_onClick()" />
<input id="txtNum" type="text"> 
<input id="btnCheck" type="button" value="Check" onclick="btnCheck_onClick()" disabled />
<p id="parMsg"></p>
<p id="lblrdm" type="text"" ></p>
izkcnapc

izkcnapc1#

函数的粗略实现

const getString = (value, size, next) => {
    let stringValue = "";

    switch (value) {
        case 1:
            stringValue = "One";
            break;
        case 2:
            stringValue = "Two";
            break;
        case 3:
            stringValue = "Three";
            break;
        case 4:
            stringValue = "Four";
            break;
        case 5:
            stringValue = "Five";
            break;
        case 6:
            stringValue = "Six";
            break;
        case 7:
            stringValue = "Seven";
            break;
        case 8:
            stringValue = "Eight";
            break;
        case 9:
            stringValue = "Nine";

        default:
            break;
    }

    if (size === "Ten") {
        switch (value) {
            case 2:
                stringValue = "Twenty";
                break;
            case 3:
                stringValue = "Thirty";
                break;
            case 4:
                stringValue = "Forty";
                break;
            case 5:
                stringValue = "Fifty";
                break;
            case 6:
                stringValue = "Sixty";
                break;
            case 7:
                stringValue = "Seventy";
                break;
            case 8:
                stringValue = "Eighty";
                break;
            case 9:
                stringValue = "Ninety";
            default:
                break;
        }
    }

    if (size === "Hundred" && next) {
        return `${stringValue} ${size} and`;
    } else if (size === "Hundred") {
        return `${stringValue} ${size}`;
    } else {
        return stringValue;
    }
};

const convertToWords = (value, old) => {
    let size;
    let num;
    let next;

    if (!value) {
        return "";
    }

    switch (value.toString().length) {
        case 2:
            size = "Ten";
            next = value % 10;
            num = Math.floor(value / 10);
            break;
        case 3:
            size = "Hundred";
            next = value % 100;
            num = Math.floor(value / 100);
            break;
        default:
            size = "Unit";
            next = 0;
            num = value;
            break;
    }

    let response;
    if (num === 1 && size === "Ten") {
        switch (next) {
            case 1:
                response = "Eleven";
                next = 0;
                break;
            case 2:
                response = "Twelve";
                next = 0;
                break;
            case 3:
                response = "Thirteen";
                next = 0;
                break;
            case 4:
                response = "Fourteen";
                next = 0;
                break;
            case 5:
                response = "Fifteen";
                next = 0;
                break;
            case 6:
                response = "Sixteen";
                next = 0;
                break;
            case 7:
                response = "Seventeen";
                next = 0;
                break;
            case 8:
                response = "Eighteen";
                next = 0;
                break;
            case 9:
                response = "Nineteen";
                next = 0;
            default:
                response = "Ten";
                next = 0;
                break;
        }
    } else {
        response = getString(num, size, next);
    }

    if (old) response = `${old} ${response}`;

    if (next) return convertToWords(next, response);

    return response;
};

console.log(convertToWords(999));
// Nine Hundred and Ninety Nine

相关问题