2017-03-29 8 views
0

指定したプレース値に丸められる数値を生成するテーブルを作成しようとしています。表(HTMLコード内)は次のとおりです。If文を使用するときのトラブル

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

私はこれを行うために使用していたJavaScriptファイルには、次のとおりです。

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

のJavaScriptファイルの作品の最初の部分。テーブルの各行に場所の値(千、数百、数十など)を追加します。しかし、If Statementをインクルードして、小数点以下の桁数と適切な数を決定すると、出力は空白のテーブルになります。誰かが私が間違っていることを指摘し、それを修正する方法を示唆することができれば感謝します。

+1

if文の条件全体を括弧内でなければなりません...あなたが欠落しているコード/タイプミスのたくさん持っています。 (あなたの 'else if'条件を参照してください) –

+1

@MikeMcCaughanが言ったことは、' else if(x> = 3)&&(x <5) '' '' else if(x> = 3)&& (x <5)){' –

+0

ブラウザのコンソールを開くと、すぐに正しい場所に向かうでしょう... – jcaron

答えて

0

は、ここではそれが

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>

関連する問題