Javascript - 功能没有激活?

时间:2014-11-24 02:23:23

标签: javascript function innerhtml

我遇到了问题,我正在做一个更新html页面上的表格的程序,从用户那里获取一定数量的骰子,然后使用数组来保存滚动数字的频率并计算他们计入用户总数的百分比。

我似乎遇到了一个问题但是,我的调试器显示我的提示消息是从用户获取一个值,该数组初始化为0,但该数组似乎没有像我应该的那样通过我的函数更新。最重要的是,我正在测试更新id表数据部分以准备显示这些数据,但似乎没有显示。

有人能让我知道我到底做错了什么吗?我知道函数通常由按钮激活,但我不确定默认情况下提示按钮是否执行此操作。

非常感谢任何帮助,谢谢!

更新:在下面的评论的帮助下,代码更新,阵列正在计算卷。哇噢!我似乎仍无法将内容打印到html页面上的表格数据标签中。我会尝试将它放在函数中,看看它是否有帮助。

 // Prompts the user to set the amount of desired rolls
var rolls = prompt("How many rolls of the dice do you want?", "");

// Declare and initialize the frequencies array to 0
var frequencies = new Array(13);

for (var index = 2; index <= 12; index++) 
    {
        frequencies[index] = 0;
    }

// Determine if user entered a valid number of rolls to make
if (rolls != null)
    {
        calculateRolls();
    }
else
    {
        rolls = prompt("You did not enter a valid number. Please try again.", "");
    }

// Rolls the die and stores the frequency of rolls
function calculateRolls() {

for (var i = 0; i < rolls; i++)
    {
        var roll1 = Math.floor(Math.random() * 6) + 1;
        var roll2 = Math.floor(Math.random() * 6) + 1;
        sum = roll1 + roll2;
        frequencies[sum]++;
    }

}

// Display frequency and percentage of rolls on the main page
 var freq2 = document.getElementById("f2");
 freq2.innerHTML = "test array should go here";

这里也是HTML代码,f2是表数据的id。我尽力保持我教授指定的格式,所以表格有点长。

......
<script src="Lab8.js" type="text/javascript"></script>
</head>

<body>

<div id="mainDiv">

<h1>Lab 8: JavaScript</h1>

<!--This table holds the rolled data-->
<table>
<caption id="rollsCap">Frequency for x Rolls</caption>
<tr>
<th>Value Rolled</th>
<th>Frequency</th>
<th>Percentage</th>
</tr>
<tr>
<th>2</th>
<td id="f2"></td>
<td id="p2"></td>
</tr>
<tr>
<th>3</th>
<td id="f3"></td>
<td id="p3"></td>
</tr>
<tr>
<th>4</th>
<td id="f4"></td>
<td id="p4"></td>
</tr>
<tr>
<th>5</th>
<td id="f5"></td>
<td id="p5"></td>
</tr>
<tr>
<th>6</th>
<td id="f6"></td>
<td id="p6"></td>
</tr>
<tr>
<th>7</th>
<td id="f7"></td>
<td id="p7"></td>
</tr>
<tr>
<th>8</th>
<td id="f8"></td>
<td id="p8"></td>
</tr>
<tr>
<th>9</th>
<td id="f9"></td>
<td id="p9"></td>
</tr>
<tr>
<th>10</th>
<td id="f10"></td>
<td id="p10"></td>
</tr>
<tr>
<th>11</th>
<td id="f11"></td>
<td id="p11"></td>
</tr>
<tr>
<th>12</th>
<td id="f12"></td>
<td id="p12"></td>
</tr>
<tr>
<td>Percentages are rounded</td>
</tr>
</table>

</div>

</body>

</html>

0 个答案:

没有答案
相关问题