2017-03-29 3 views
0

Ich versuche, eine Tabelle zu erstellen, die Zahlen generiert, die auf einen angegebenen Stellenwert gerundet werden. Die Tabelle (im HTML-Code) ist unten abgebildet.Problem mit einer If-Anweisung

 <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> 

Die JavaScript-Datei, die ich, dies zu tun bin mit ist:

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); 
    } 
} 

Der erste Teil der JavaScript-Datei funktioniert. Es fügt den Tabellenwert (Tausender, Hunderter, Zehner usw.) zu jeder Zeile der Tabelle hinzu. Wenn ich jedoch die If-Anweisung einschließe, um die Anzahl und die richtige Anzahl von Dezimalstellen zu bestimmen, ist die Ausgabe eine leere Tabelle, die nicht gefüllt wird. Ich würde mich freuen, wenn jemand darauf hinweisen könnte, was ich falsch mache und vorschlage, wie ich es beheben kann.

+1

Die gesamte Bedingung einer if-Anweisung muss in Klammern sein. (Siehe Ihre 'else if' Bedingung. –

+1

Was @MikeMcCaughan sagte, Sie müssen'} ändern else if (x> = 3) && (x <5) {'to'} else if ((x> = 3) && (x <5)) {' –

+0

Das Öffnen der Konsole Ihres Browsers hätte Sie wahrscheinlich sofort auf den richtigen Platz gebracht ... – jcaron

Antwort

0

hier ist es, Ihnen eine Menge fehlenden Code/Tippfehler bekam ...

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); 
 
} 
 
}
<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><span id="number1"></span></td> 
 
    </tr> 
 
    <tr> 
 
    <td>b.</td> 
 
    <td><span id="roundingQuestion2"></span></td> 
 
    <td><span id="placeValue2"></span></td> 
 
    <td><span id="number2"></span></td> 
 
    </tr> 
 
    <tr> 
 
    <td>c.</td> 
 
    <td><span id="roundingQuestion3"></span></td> 
 
    <td><span id="placeValue3"></span></td> 
 
    <td><span id="number3"></span></td> 
 
    </tr> 
 
    <tr> 
 
    <td>d.</td> 
 
    <td><span id="roundingQuestion4"></span></td> 
 
    <td><span id="placeValue4"></span></td> 
 
    <td><span id="number4"></span></td> 
 
    </tr> 
 
    <tr> 
 
    <td>d.</td> 
 
    <td><span id="roundingQuestion5"></span></td> 
 
    <td><span id="placeValue5"></span></td> 
 
    <td><span id="number5"></span></td> 
 
    </tr> 
 
    <tr> 
 
    <td>e.</td> 
 
    <td><span id="roundingQuestion6"></span></td> 
 
    <td><span id="placeValue6"></span></td> 
 
    <td><span id="number6"></span></td> 
 
    </tr> 
 
    <tr> 
 
    <td>f.</td> 
 
    <td><span id="roundingQuestion7"></span></td> 
 
    <td><span id="placeValue7"></span></td> 
 
    <td><span id="number7"></span></td> 
 
    </tr> 
 
    <tr> 
 
    <td>g.</td> 
 
    <td><span id="roundingQuestion8"></span></td> 
 
    <td><span id="placeValue8"></span></td> 
 
    <td><span id="number8"></span></td> 
 
    </tr> 
 
    <tr> 
 
    <td>h.</td> 
 
    <td><span id="roundingQuestion9"></span></td> 
 
    <td><span id="placeValue9"></span></td> 
 
    <td><span id="number9"></span></td> 
 
    </tr> 
 
    <tr> 
 
    <td>i.</td> 
 
    <td><span id="roundingQuestion10"></span></td> 
 
    <td><span id="placeValue10"></span></td> 
 
    <td><span id="number10"></span></td> 
 
    </tr> 
 

 
</table> 
 

 
<br /> 
 
<br /> 
 

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