I've been working on a JavaScript program lately. It's supposed to randomly select two characters from Once Upon A Time and match them up. The randomizing part is fine, but I'm struggling with the "Add Name to Array" feature. Here's my code snippet:
<center>
<b>
<input type="button" value="Randomize!" onclick="ouatRandomizer();">
<b>
<p id="text"></p>
<input id="name" type="text" placeholder="Add a Name" />
<input type="button" value="Add Array" onclick="addToArray();">
</center>
<script>
var nameInput = document.getElementById("name");
var names = ["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 nameone = names[Math.floor(Math.random() * names.length)];
var nametwo = names[Math.floor(Math.random() * names.length)];
message = "Your characters are.. " + nameone + " and " + nametwo + ".";
function ouatRandomizer() {
nameone = names[Math.floor(Math.random() * names.length)];
nametwo = names[Math.floor(Math.random() * names.length)];
message = "Your characters are.. " + nameone + " and " + nametwo + ".";
document.getElementById("text").innerHTML = message;
}
function addToArray( name ) {
names.push( name );
console.log(names.join());
}
</script>
I want the program to accept any name in the input box and automatically incorporate it into the randomization process. It doesn't need to store the name permanently. If that's possible, great!
Thank you for taking the time to address my issue and provide help. Just a couple of things to note: I'm new to these forums and not very familiar with them yet, so please bear with me. Also, if you can explain the code briefly but clearly, as I'm still learning and navigating this environment.
Thanks again!