2017-03-29 2 views
0

숫자가 지정된 자리 값으로 반올림되는 테이블을 만들려고합니다. 표 (HTML 코드)가 아래에 나와 있습니다.Trouble 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> 

내가이 일을 사용하고 자바 스크립트 파일은 다음과 같습니다

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

자바 스크립트 파일 작품의 첫 부분. 테이블의 각 행에 자리 값 (수천, 수백, 수십 등)을 추가합니다. 그러나 If 문을 사용하여 숫자와 소수 자릿수를 결정할 때 결과는 채워지지 않을 빈 테이블입니다. 누군가 내가 잘못하고있는 것을 지적하고 그것을 고치는 방법을 제안 할 수 있다면 고맙겠습니다.

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