KBuff
KBuff

Reputation: 13

why is my function if/else if/ else if statement returning the same answer?

I have a rock, paper, scissors game set up with the user clicking on the buttons labeled "Rock", "Paper", "Scissors" which results in the userChoice. When I run the program the compareChoices() function always returns "The result is a tie, let's play again!", I don't understand why.

  < article >
    < button onclick = "rockPaperScissors('Rock')" > Rock < /button>
    <button onclick="rockPaperScissors('Paper')">Paper</button >
    < button onclick = "rockPaperScissors('Scissors')" > Scissors < /button>
  </article >

    < script type = "text/javascript" >
    function rockPaperScissors(userchoice) {
      alert("You chose " + userchoice + " ...the computer chose " + getComputerChoice() + ".");
      compareChoices();
    }

  function getComputerChoice() {
    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
      computerChoice = "Rock";
    } else if (computerChoice < 0.67) {
      computerChoice = "Paper";
    } else {
      computerChoice = "Scissors";
    }
    return computerChoice;
  }

  function compareChoices(userChoice, ComputerChoice) {
    if (userChoice === ComputerChoice) {
      alert("The result is a tie, let's play again!");
    } else if (userChoice === "Rock") {
      if (ComputerChoice === "Scissors") {
        alert("Congratulations, you win!");
      } else {
        alert("The computer wins! Care to play again?");
      }
    } else if (userChoice === "Scissors") {
      if (ComputerChoice === "Rock") {
        alert("The computer wins, let's play again!");
      } else {
        alert("Yippie! You win!");
      }
    } else if (userChoice === "Paper") {
      if (ComputerChoice === "Rock") {
        alert("The computer wins. Don't give up, try again!");
      } else {
        alert("Hail the all mighty visitor. Give it another go!");
      }
    }
  } < /script>

Upvotes: 0

Views: 126

Answers (3)

jrkt
jrkt

Reputation: 2715

You need to pass the arguments to compareChoices().

function rockPaperScissors(userchoice) {
      alert("You chose " + userchoice + " ...the computer chose " + getComputerChoice() + ".");
      compareChoices(userchoice, getComputerChoice());
    }

You should only alert one time since they are going to come so close together. Something like this:

function compareChoices(userChoice, ComputerChoice) {
    var message;
    if (userChoice === ComputerChoice) {
      message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The result is a tie, let's play again!";
    } else if (userChoice === "Rock") {
      if (ComputerChoice === "Scissors") {
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - Congratulations, you win!";
      } else {
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The computer wins! Care to play again?");
      }
    } else if (userChoice === "Scissors") {
      if (ComputerChoice === "Rock") {
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The computer wins, let's play again!";
      } else {
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - Yippie! You win!";
      }
    } else if (userChoice === "Paper") {
      if (ComputerChoice === "Rock") {
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The computer wins. Don't give up, try again!";
      } else {
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - Hail the all mighty visitor. Give it another go!";
      }
    }
  alert(message);
}

Upvotes: 1

Azad
Azad

Reputation: 5264

you have to put the computerchoice into variable, and call comparechoices function with same computerchoice.

function rockPaperScissors(userchoice) {
       var computerchoice = getComputerChoice();
       alert("You chose " + userchoice + " ...the computer chose " + computerchoice + ".");
       compareChoices(userchoice, computerchoice);
    }

Upvotes: 1

mgiesa
mgiesa

Reputation: 1013

You aren't passing anything into compareChoices

  < article >
    < button onclick = "rockPaperScissors('Rock')" > Rock < /button>
    <button onclick="rockPaperScissors('Paper')">Paper</button >
    < button onclick = "rockPaperScissors('Scissors')" > Scissors < /button>
  </article >

    < script type = "text/javascript" >
    function rockPaperScissors(userchoice) {
      var computerChoice = getComputerChoice();
      alert("You chose " + userchoice + " ...the computer chose " + computerChoice + ".");
      compareChoices(userChoice, computerChoice);
    }

  function getComputerChoice() {
    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
      computerChoice = "Rock";
    } else if (computerChoice < 0.67) {
      computerChoice = "Paper";
    } else {
      computerChoice = "Scissors";
    }
    return computerChoice;
  }

  function compareChoices(userChoice, ComputerChoice) {
    if (userChoice === ComputerChoice) {
      alert("The result is a tie, let's play again!");
    } else if (userChoice === "Rock") {
      if (ComputerChoice === "Scissors") {
        alert("Congratulations, you win!");
      } else {
        alert("The computer wins! Care to play again?");
      }
    } else if (userChoice === "Scissors") {
      if (ComputerChoice === "Rock") {
        alert("The computer wins, let's play again!");
      } else {
        alert("Yippie! You win!");
      }
    } else if (userChoice === "Paper") {
      if (ComputerChoice === "Rock") {
        alert("The computer wins. Don't give up, try again!");
      } else {
        alert("Hail the all mighty visitor. Give it another go!");
      }
    }
  } < /script>

Upvotes: 2

Related Questions