mardi 1 mai 2018

How to run custom random function in a for loop

I am trying to print 10 random names, but the loop is not generating them - only one name appears. I don't know hot to call my Randomizer function in the for loop properly. So far I have tried this:

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



Aucun commentaire:

Enregistrer un commentaire