Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code has quite a few problems. But the problem you are asking about stems from the way you are doing comparison.</p> <p>You cannot do this:</p> <pre><code>if (player1Choice &amp;&amp; player2Choice == "Rock") </code></pre> <p>What that meant is essentially:</p> <pre><code>if ((player1Choice == true) &amp;&amp; (player2Choice == "Rock")) </code></pre> <p>Instead, you want to write it this way (but it will still not work because of many other errors):</p> <pre><code>if (player1Choice == player2Choice) { $("resultOutput").value = "Both players chose " + player1Choice + ", which results in a stalemate"; } </code></pre> <p>Not only do you have less comparison operations, you also save many lines of code!</p> <p>Do note that you have additional typos in the last 2 comparisons where you added ".value" erroneously.</p> <p>On top of that, you might want to note that the functions <code>player1Choice</code> and <code>player2Choice</code> are not variables. You have specified for them to be event handlers for click events. The values they return go no where and will not be received by the <code>fightcrunch</code> function.</p> <p>I do not really want to spoil your fun with making this program, but if you do give up, you can see corrected and functional code here (it is tabbed to the right in case you do not want to see it):</p> <pre><code> &lt;form&gt; Player 1 &lt;select id="player1"&gt; &lt;option value="0" selected&gt;Paper&lt;/option&gt; &lt;option value="1"&gt;Rock&lt;/option&gt; &lt;option value="2"&gt;Scissors&lt;/option&gt; &lt;/select&gt; Player 2 &lt;select id="player2"&gt; &lt;option value="0" selected&gt;Paper&lt;/option&gt; &lt;option value="1"&gt;Rock&lt;/option&gt; &lt;option value="2"&gt;Scissors&lt;/option&gt; &lt;/select&gt; &lt;input type="submit" id="fight" value="Fight"&gt; &lt;/form&gt; &lt;div id="resultOutput"&gt;&lt;/div&gt; &lt;script&gt; // create our $() shortcut function for easily retrieving elements by id var $ = function(id) { return document.getElementById(id); } //function executed on page load window.onload = function() { //clear any previous values document.forms[0].reset(); $("fight").onclick = fightCrunch; } var fightCrunch = function(){ var choices = ["Paper", "Rock", "Scissors"]; var player1 = $("player1").value; var player2 = $("player2").value; var result = ""; var diff = player1 - player2; if (!diff) result = "Both players chose " + choices[player1] + ", which results in a stalemate"; else if (diff == -1 || diff == 2) result = "Player 1's " + choices[player1] + " beats Player 2's " + choices[player2]; else result = "Player 2's " + choices[player2] + " beats Player 1's " + choices[player1]; $("resultOutput").innerHTML = result; return false; } &lt;/script&gt; </code></pre> <p>Good luck!</p> <p>Edit: Using return and global variables</p> <pre><code>var player1Choice, player2Choice; window.onload = function () { //clear any previous values document.forms[0].reset(); //store value from player1Weapon radio choice with the getPlayer1Choice function $("player1Submit").onclick = function () { player1Choice = getPlayer1Choice(); }; //store value from player1Weapon radio choice with the getPlayer2Choice function $("player2Submit").onclick = function () { player2Choice = getPlayer2Choice(); }; //assign the fight button to run the fightCrunch function to determine the winner $("fight").onclick = fightCrunch; } function getPlayer1Choice () { //... // return ...; } function getPlayer2Choice () { //... // return ...; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload