0

I'm currently working on a HTML / JavaScript-based Rock Paper Scissors game, starting with the good ol' Big Bang Theory "Rock Paper Scissors Lizard Spock" variant. I plan to add the rest of the variants based on James Lovelaces's variants (I suggest you look it up - it's insane.)

Anyways, there's a problem with one of the functions I'm working with to build the game. It's supposed to add a series of buttons based on an array of Strings. I'm not sure if I'm calling it wrong on the RPS5() function, or if the adoptions function is wrong, but can someone help me out here? Thank you!

Code:

var choices5 = ["Rock", "Scissors", "Lizard", "Paper", "Spock"];

function addOptions(choiceArray) {
  var docFrag = document.createDocumentFragment();
  for (var i = 0; i < choiceArray.length; i++) {
    var elem = document.createElement('input');
    elem.type = 'button';
    elem.value = choiceArray[i];
    docFrag.appendChild(elem);
    elem.addEventListener("click", RPS5)
  }
  document.body.appendChild(docFrag);
}
function RPS5() {
  addOptions(choices5);
}
<body onload="RPS5()">
    <p id="outcome"> Outcome </p>
</body>
</html>
George
  • 2,330
  • 3
  • 15
  • 36
AidanM
  • 57
  • 9
  • you have a missing }, maybe in place of "enter code here" – George Mar 12 '18 at 22:45
  • Guess I forgot to copy the whole function. Updated it accordingly, but that's not the issue on my end.. – AidanM Mar 12 '18 at 22:47
  • 1
    clicking on any of those buttons will add all those buttons again. – George Mar 12 '18 at 22:47
  • so what is the issue ? – George Mar 12 '18 at 22:49
  • Yeah, I noticed that. I'm currently working on implementing a function that can help with that, depending on the "game" that the user wants to play. Thanks for pointing that out, though! – AidanM Mar 12 '18 at 22:52
  • 1
    "Anyways, there's a problem with one of the functions I'm working with to build the game. It's supposed to add a series of buttons based on an array of Strings." – George Mar 12 '18 at 22:52
  • I guess you helped solve part of it. Now I just need to figure out how to stop it from adding more buttons whenever a button is clicked. – AidanM Mar 12 '18 at 22:56
  • i literally solved nothing – George Mar 12 '18 at 22:56
  • 1
    One simple way to keep it from recreating the buttons on each iteration would be to put them into their own container and give it a unique id or class so you can easily [delete the element](https://stackoverflow.com/questions/3387427/remove-element-by-id). Another unrequested suggestion which may help would be to put the choices in order to simplify checking for the win. – l3l_aze Mar 12 '18 at 23:14

0 Answers0