mercredi 29 mars 2017

Trouble Using an If Statement

I am trying to create a table that will generate numbers to round to a specified place value. The table (in HTML code) is shown below.

<head>

    <title>Rounding Worksheet</title>

    <script src = "C:\Users\bryan\Documents\Bryan\Web Pages\Math Macros\math.js"></script>

</head>

<body>

    <table border = "1">

        <col width = "25">
        <col width = "150">
        <col width = "100">
        <col width = "100">
        <col width = "50">

        <tr><td>  </td><td>Place Vaue</td><td>Value</td><td></td></tr>
        <tr><td>a.</td><td><span id = "roundingQuestion1"></span></td><td><span id = "placeValue1"></span></td><td></td></tr>
        <tr><td>b.</td><td><span id = "roundingQuestion2"></span></td><td><span id = "placeValue2"></span></td><td></td></tr>
        <tr><td>c.</td><td><span id = "roundingQuestion3"></span></td><td><span id = "placeValue3"></span></td><td></td></tr>
        <tr><td>d.</td><td><span id = "roundingQuestion4"></span></td><td><span id = "placeValue4"></span></td><td></td></tr>
        <tr><td>d.</td><td><span id = "roundingQuestion5"></span></td><td><span id = "placeValue5"></span></td><td></td></tr>
        <tr><td>e.</td><td><span id = "roundingQuestion6"></span></td><td><span id = "placeValue6"></span></td><td></td></tr>
        <tr><td>f.</td><td><span id = "roundingQuestion7"></span></td><td><span id = "placeValue7"></span></td><td></td></tr>
        <tr><td>g.</td><td><span id = "roundingQuestion8"></span></td><td><span id = "placeValue8"></span></td><td></td></tr>
        <tr><td>h.</td><td><span id = "roundingQuestion9"></span></td><td><span id = "placeValue9"></span></td><td></td></tr>
        <tr><td>i.</td><td><span id = "roundingQuestion10"></span></td><td><span id = "placeValue10"></span></td><td></td></tr>

    </table>        

    <br />
    <br />

    <button onclick="createRoundingQuestions()">Create Questions</button>

</body>

The javascript file that I am using to do this is:

var placeValueName = ["Thousandth", "Hundredth", "Tenth", "Ones", "tens", "Hundreds", "Thousands", "Ten Thousands", "Hundred Thousands"];

function getRndInteger(min, max){

return Math.floor(Math.random() * (max - min) ) + min;

}

function createRoundingQuestions(){

var j;
var x;
var numberDecimals;
var lowValue;
var highValue;

for (j = 1; j <= 10; j++){

    x = getRndInteger(0, placeValueName.length, 0);

    document.getElementById("roundingQuestion"+j).innerHTML = placeValueName[x]; 
    document.getElementById("number"+j).innerHTML = x;

    if (x >= 5){

        lowValue = Math.pow(10, x - 3);
        highValue = Math.pow(10, x - 2);
        numberDecimals = getRndInteger(0, 6);


    } else if (x >= 3) && (x < 5){

        lowValue = 1;
        highValue = 1000;
        numberDecimals = getRndInteger(1, 3);


    } else {

        lowValue = 1;
        highValue = 1000;
        numberDecimals = getRndInteger(2, 6);


    }


    document.getElementById("placeValue"+j).innerHTML = (Math.random() * (highValue - lowValue) ) + lowValue).tofixed(numberDecimals);


}

}

The first part of the javascript file works. It will add the place value (thousands, hundreds, tens, etc.) to each row of the table. However, when I include the If Statement to detemine the number and the proper number of decimal places, the output is a blank table that will not populate. I would appreciate if anyone could point out what I am doing wrong and suggest how to fix it.

Aucun commentaire:

Enregistrer un commentaire