我正在尝试创建一个表格,该表格将生成数字以舍入到指定的位置值。表格(HTML代码)如下所示。
<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语句来确定数字和正确的小数位数时,输出是一个不会填充的空白表。如果有人能够指出我做错了什么并建议如何解决它,我将不胜感激。
答案 0 :(得分: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>