这个数字到单词转换有什么问题?

时间:2018-05-10 08:49:08

标签: javascript

<script>
var iWords = ['Zero', ' One', ' Two', ' Three', ' Four', ' Five', ' Six', ' Seven', ' Eight', ' Nine'];
var ePlace = ['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
var tensPlace = ['', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety'];
var inWords = [];
var numReversed, inWords, actualnum, i, j;
function test() {
    'use strict';
    if (actualnum[i] === 0) {
        inWords[j] = '';
    } else if (actualnum[i] === 1) {
        inWords[j] = ePlace[actualnum[i - 1]];
    } else {
        inWords[j] = tensPlace[actualnum[i]];
    }
}
function testSkill() {
    'use strict';
    var junkVal = document.getElementById('vals').value;
    junkVal = Math.floor(junkVal);
    var obStr = junkVal.toString();
    numReversed = obStr.split('');
    actualnum = numReversed.reverse();
    if (Number(junkVal) >= 0) {
        //do nothing
    } else {
        window.alert('wrong Number cannot be converted');
        return false;
    }
    if (Number(junkVal) === 0) {
        document.getElementById('container').innerHTML = obStr + '' + ' Zero ';
        return false;
    }
    if (actualnum.length > 9) {
        window.alert('Oops!!!! the Number is too big to covertes');
        return false;
    }
    var iWordsLength = numReversed.length;
    var finalWord = '';
    j = 0;
    for (i = 0; i < iWordsLength; i++) {
        switch (i) {
            case 0:
                if (actualnum[i] === '0' || actualnum[i + 1] === '1') {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[actualnum[i]];
                }
                inWords[j] = inWords[j] + ' ';
                break;
            case 1:
                test();
                break;
            case 2:
                if (actualnum[i] === '0') {
                    inWords[j] = '';
                } else if (actualnum[i - 1] !== '0' && actualnum[i - 2] !== '0') {
                    inWords[j] = iWords[actualnum[i]] + ' Hundred and';
                } else {
                    inWords[j] = iWords[actualnum[i]] + ' Hundred';
                }
                break;
            case 3:
                if (actualnum[i] === '0' || actualnum[i + 1] === '1') {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[actualnum[i]];
                }
                if (actualnum[i + 1] !== '0' || actualnum[i] > '0') {
                    inWords[j] = inWords[j] + ' Thousand';
                }
                break;
            case 4:
                test();
                break;
            case 5:
                if (actualnum[i] === '0' || actualnum[i + 1] === '1') {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[actualnum[i]];
                }
                if (actualnum[i + 1] !== '0' || actualnum[i] > '0') {
                    inWords[j] = inWords[j] + ' Lakh';
                }
                break;
            case 6:
                test();
                break;
            case 7:
                if (actualnum[i] === '0' || actualnum[i + 1] === '1') {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[actualnum[i]];
                }
                inWords[j] = inWords[j] + ' Crore';
                break;
            case 8:
                test();
                break;
            default:
                break;
        }
        j++;
    }
    inWords.reverse();
    for (i = 0; i < inWords.length; i++) {
        finalWord += inWords[i];
    }
    document.getElementById('container').innerHTML = obStr + '  ' + finalWord;
}
</script>

每次用户输入11-19的数字时,输出总是&#34; TEN&#34;。如果我把它15作为十。我试过改变循环但事情变得更糟。我的一些尝试甚至不会做任何改变,即使它看起来是一个很大的变化,而其他人继续制造自己的新问题。

1 个答案:

答案 0 :(得分:1)

使用==运算符代替===

中的function test()

请注意,标识(===)运算符的行为与等于(==)运算符完全相同,但不进行类型转换,类型必须为同样被认为是平等的。由于actualnum数组的类型为char,因此您尝试将其与数字进行比较,因此条件失败。