如何在for循环中运行自定义随机函数

时间:2018-05-01 21:25:59

标签: javascript loops for-loop random

我正在尝试打印10个随机名称,但循环不生成它们 - 只显示一个名称。我不熟悉在for循环中正确调用<center> <b> <input type="button" value="Generate database" onclick="Randomizer();"> <b> <table> <tr id="labels"><td>First Name</td><td>Last Name</td></tr> <tr id="fname"></tr></table> </center> <script> var firstnames = ["Hook", "Rumpelstiltskin", "Belle", "Emma", "Regina", "Aurora", "Elsa", "Anna", "Snow White", "Prince Charming", "Cora", "Zelena", "August", "Mulan", "Graham", "Discord", "Will", "Robin Hood", "Jiminy Cricket", "Henry", "Neal", "Red"]; var lastnames = ["Adams","Bowden","Conway","Darden","Edwards","Flynn","Gilliam","Holiday","Ingram","Johnson","Kraemer","Hunter","McDonald","Nichols","Pierce","Sawyer","Saunders","Schmidt","Schroeder","Smith","Douglas","Ward","Watson","Williams","Winters"]; var count = 10; function Randomizer() { nameone = firstnames[Math.floor(Math.random() * firstnames.length)]; nametwo = lastnames[Math.floor(Math.random() * lastnames.length)]; message = "<td>" + nameone + "</td><td>" + nametwo + "</td>"; document.getElementById("fname").innerHTML = message; } for (var i = 1; i < count; i++) { Randomizer(); } </script>函数。到目前为止,我已经尝试过这个:

&#13;
&#13;
[onshow;if [onload.project.sortBy]='Id';then if [onload.project.sortAscending]=1;then '↓';else '↑']
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

每次循环迭代时,此行都会覆盖DB::raw('count(distinct student_id) as Students') 元素(tr)中的所有内容:

<tr id="fname">

你生成了多个名字;除了最后一个,你只需要消灭掉。

您需要为每个名称添加一个新行。

答案 1 :(得分:0)

我创建了一个解决方案,可以在单击按钮时清除上一次运行。此外,我将循环放在函数中,这样它就不会在页面加载时执行,而是单击鼠标。

&#13;
&#13;
var firstnames = ["Hook", "Rumpelstiltskin", "Belle", "Emma", "Regina", "Aurora", "Elsa", "Anna", "Snow White", "Prince Charming", "Cora", "Zelena", "August", "Mulan", "Graham", "Discord", "Will", "Robin Hood", "Jiminy Cricket", "Henry", "Neal", "Red"];
var lastnames = ["Adams", "Bowden", "Conway", "Darden", "Edwards", "Flynn", "Gilliam", "Holiday", "Ingram", "Johnson", "Kraemer", "Hunter", "McDonald", "Nichols", "Pierce", "Sawyer", "Saunders", "Schmidt", "Schroeder", "Smith", "Douglas", "Ward", "Watson", "Williams", "Winters"];
var count = 10;

function createRowFromHTML(htmlString) {
  var table = document.createElement('tbody');
  table.innerHTML = htmlString.trim();

  return table.firstChild;
}

function Randomizer() {
	// clear old names
  document.getElementsByTagName("tbody")[0].innerHTML = "";

  for (var i = 1; i < count; i++) {
    nameone = firstnames[Math.floor(Math.random() * firstnames.length)];
    nametwo = lastnames[Math.floor(Math.random() * lastnames.length)];
    message = "<tr><td>" + nameone + "</td><td>" + nametwo + "</td></tr>";
    var row = window.createRowFromHTML(message)
    document.getElementsByTagName("tbody")[0].appendChild(row);
  }
}
&#13;
<center>
  <input type="button" value="Generate database" onclick="Randomizer();">

  <table id="names">
    <thead>
      <tr>
        <th>First Name</th>
        <th>Last Name</th>
      </tr>
    </thead>
    <tbody>

    </tbody>
  </table>
</center>
&#13;
&#13;
&#13;