因此,我代码中的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>
1条答案
按热度按时间izkcnapc1#
函数的粗略实现