1

So the HTML in my code is just part of creating a basic game that generates a random number and turns it to a word, so the user has to type in the number its it numerical form.

Can anyone help as I can't seem to get my head around how to make the conversion work or suggest a better way?

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>
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
Lawrence
  • 11
  • 2

0 Answers0