Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems getting the right value returned in a javascript function
    primarykey
    data
    text
    <p>What I'm trying to do is simulate a simple paper, rock, scissors game within a page, where one player selects r/p/s with radio buttons, submits his choice, and player 2 does the same.</p> <p>I have no doubt there are multiple problems to this code, but I see really weird stuff returned whenever I try and run the function that resolves the rock/paper/scissors. I always get a true on the:</p> <pre><code>else if (player1Choice("Rock") &amp;&amp; player2Choice("Scissors")) { $("resultOutput").value = "Player 1s Rock beats Player 2s Scissors"; } </code></pre> <p>I get this back as my result every time. I've stared at the function so long I'm probably just blind to an obvious resolution at this point.</p> <pre><code>// 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(); //store value from player1Weapon radio choice with the player1Choice function $("player1Submit").onclick = player1Choice; //store value from player1Weapon radio choice with the player2Choice function $("player2Submit").onclick = player2Choice; //assign the fight button to run the fightCrunch function to determine the winner $("fight").onclick = fightCrunch; } var player1Choice = function(x){ var a = "Paper"; var b = "Rock"; var c = "Scissors"; //the html has several radio buttons with id's of "player1Paper", etc. the $ function is to get the id name, and then I'm doing if, else ifs to see which on is checked. if ($("player1Paper").checked) { return a; } else if ($("player1Rock").checked) { return b; } else if ($("player1Scissors").checked) { return c; } else { alert("Player 1, you need to pick a crude instrument of violence first"); } }; var player2Choice = function(y){ var d = "Paper"; var e = "Rock"; var f = "Scissors"; //var d = $("player2Paper").value; //var e = $("player2Rock").value; //var f = $("player2Scissors").value; if ($("player2Paper").checked) { return d; } else if ($("player2Rock").checked) { return e; } else if ($("player2Scissors").checked) { return f; } else { alert("Player 2, you need to pick a crude instrument of violence first"); } }; var fightCrunch = function(){ //The next few lines are commented out because the if gets hung up on this part, this always comes back as true no matter what I do with the previous functions. //if (player1Choice || player2Choice == undefined) { //alert("Both players need to select and submit a weapon of choice before a winner is determined"); //} if (player1Choice &amp;&amp; player2Choice == "Rock") { $("resultOutput").value = "Both players chose Rock, which results in a stalemate"; } else if (player1Choice &amp;&amp; player2Choice == "Paper") { $("resultOutput").value = "Both players chose Paper, which results in a stalemate"; } else if (player1Choice &amp;&amp; player2Choice == "Scissors") { $("resultOutput").value = "Both players chose Scissors, which results in a stalemate"; } else if (player1Choice("Rock") &amp;&amp; player2Choice("Scissors")) { $("resultOutput").value = "Player 1s Rock beats Player 2s Scissors"; } else if (player1Choice("Rock") &amp;&amp; player2Choice("Paper")) { $("resultOutput").value = "Player 2s Paper beats Player 1s Rock"; } else if (player1Choice("Paper") &amp;&amp; player2Choice("Rock")) { $("resultOutput").value = "Player 1s Paper beats Player 2s Rock"; } else if (player1Choice("Paper") &amp;&amp; player2Choice("Scissors")) { $("resultOutput").value = "Player 2s Scissors beats Player 1s Paper"; } else if (player2Choice("Paper").value &amp;&amp; player1Choice("Scissors")) { $("resultOutput").value = "Player 1s Scissors beats Player 2s Paper"; } else if (player2Choice("Rock").value &amp;&amp; player1Choice("Scissors")) { $("resultOutput").value = "Player 2s Rock beats Player 1s Scissors"; } else { alert("something is wrong here"); } } </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. 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