[Solved] Rock, paper, scissors jQuery game not working [closed]


As Robby Cornelissen said, your variable names are not valid. See this previously asked question about this topic : What characters are valid for JavaScript variable names?

That being said, you do have quite a few errors popping so before coming to us, try investigating them. That’s how you’ll make some progress, instead of having someone do the job for you.

Once those errors are solved, you get something that works (see snippet below). However your method is awfully painful and bulky. I don’t have the whole what-beats-what in rock-paper-scissors-lizard-spock, but to write elegant code, you should try to find a way to decide who is the winner without describing each particular case with an if statement.

Maybe there’s something worth looking into with having your values inside an array, comparing the indexes of your values (your user’s and the computer’s) and i’m thinking about permutation cycles on the array to have the values in the correct order.

$(function() {

  $(document).on('click', '.game-image', function(event) {
    var value = $(event.target).data('value');
    var win;
    var comproll = 1 + Math.floor(Math.random() * 5);
    //$('#comproll').html('Result: '+comproll)
    if (comproll === 1) {
      comp = "Lizard";
    } else if (comproll === 2) {
      comp = "paper";
    } else if (comproll === 3) {
      comp = "scissors";
    } else if (comproll === 4) {
      comp = "Spock";
    } else if (comproll === 5) {
      comp = "Rock";
    }

    if (value === comproll) {
      win = "This ends in a tie"
    } else if (value === 1) {
      if (comproll === 2, 4) {
        win = "You win, comp choose" + comp + ".";
      } else if (comproll === 3, 5) {
        win = "You lose, comp choose " + comp + ".";
      }
    } else if (value === 2) {
      if (comproll === 4, 5) {
        win = "You win, comp choose" + comp + ".";
      } else if (comproll === 1, 3) {
        win = "You lose, comp choose " + comp + ".";
      }
    } else if (value === 3) {
      if (comproll === 1, 2) {
        win = "You win, comp choose" + comp + ".";
      } else if (comproll === 4, 5) {
        win = "You lose, comp choose " + comp + ".";
      }
    } else if (value === 4) {
      if (comproll === 3, 5) {
        win = "You win, comp choose" + comp + ".";
      } else if (comproll === 1, 2) {
        win = "You lose, comp choose " + comp + ".";
      }
    } else if (value === 5) {
      if (comproll === 1, 3) {
        win = "You win, comp choose" + comp + ".";
      } else if (comproll === 2, 4) {
        win = "You lose, comp choose " + comp + ".";
      }

    }

    $('#win').text(win);
  }); //closes play function

}); // closes function
<body>
  <div id="wrapper">

    <h1>Lizard, paper, scissors, spock, rock</h1>

    <div id="images">
      <img class="game-image" src="https://stackoverflow.com/questions/26726179/Images/lizard.jpg" width="150" height="150" alt="" data-value="1" />
      <img class="game-image" src="Images/paper.jpg" width="150" height="150" alt="" data-value="2" />
      <img class="game-image" src="Images/scissors.jpg" width="150" height="150" alt="" data-value="3" />
      <img class="game-image" src="Images/spock.jpg" width="150" height="150" alt="" data-value="4" />
      <img class="game-image" src="Images/rock.jpg" width="150" height="150" alt="" data-value="5" />
    </div>
    <div id="win"></div>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="unit2.js"></script>
</body>

2

solved Rock, paper, scissors jQuery game not working [closed]