This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Stephen
over 8 years

How can I change this to play again if I get a Tie?

var compare = function(choice1, choice2) {
        if (choice1 === choice2) {
                return "The result is a tie! Play Again";
        }
        else if (choice1 === "Rock") {
            if (choice2 === "Scissors") {
                return "Player 1: Rock Wins";
            }
            else if (choice2 === "Paper") {
                return "Computer: Paper Wins";
            }
            else {
                return "Computer: Rope Wins";
            } 
        }
        else if (choice1 === "Paper") {
            if (choice2 === "Rock") {
                return "Player 1: Paper Wins";
            }
            else if (choice2 === "Scissors") {
                return "Computer: Scissors Wins";
            }
            else {
                return "Computer: Rope Wins"
            }
        }
        else if (choice1 === "Scissors") {
            if (choice2 === "Rock") {
                return "Computer: Rock Wins";
            }
            else if (choice2 === "Paper") {
                return "Player 1: Scissors Wins";
            }
            else {
                return "Computer: Rope Wins"
            }
        }
};

    var userChoice = prompt("Do you choose Rock, Paper or Scissors?");
        if (userChoice === "Rock") {
            userChoice = "Rock"
            console.log("Player 1s Answer: " + userChoice);
        }
        else if (userChoice === "Paper") {
            userChoice = "Paper"
            console.log("Player 1s Answer: " + userChoice);
        }
        else if (userChoice === "Scissors") {
            userChoice = "Scissors"
            console.log("Player 1s Answer: " + userChoice);
        }
        else {
            userChoice = prompt(userChoice + " is invalid input: " + 
            "Try Again: \nDo you choose Rock, Paper or Scissors?");
            console.log("Player 1s Answer: " + userChoice);
        } 
        
    var computerChoice = Math.random();
        if (computerChoice <= 0.25) {
            computerChoice = "Rock";
            console.log("Computer's Choice: " + computerChoice);
        } 
        else if(computerChoice <= 0.50) {
            computerChoice = "Paper";
            console.log("Computer's Choice: " + computerChoice);
        } 
        else if (computerChoice <= 0.75) {
            computerChoice = "Scissors";
            console.log("Computer's Choice: " + computerChoice);
        } 
        else                             
        {
            computerChoice = "Rope";
            console.log("Computer's Choice: " + computerChoice);
        }
    
    compare(userChoice, computerChoice);

Answer 55fa5404e39efe752100061c

0 votes

Permalink

I tried wrapping the computerChoice and userChoice into a function called playAgain, so I could just call that, to start the game and to replay if there is a tie, but it didn’t work, and I’m out of ideas. Any help would be much appreciated.

points
Submitted by Stephen
over 8 years

2 comments

Yehosua Selah over 8 years

I suggest that you should proceed to the next lessons, you will learn there what you are looking for, the for loops, while loops and the do..while loops.. just come back to this game if you already possess enough knowledge to improve this.. :) hope this advice helps..

Stephen over 8 years

Thanks, I did try to use a while loop for the input checking, but I couldn’t figure out how to make it NOT an infinite loop, I will definitely come back to this, after I have done the more advanced lessons. Thanks for the advice

Answer 55ff19d086f552b8ae000003

0 votes

Permalink

 if (choice1 === choice2) {
    return prompt("Choose again,rock, paper or scissors?") ;
}

I have tried this but when I try to choose again it only prints out

Computer: rock “rock”

points
Submitted by Hossam L Kommy
over 8 years

1 comments

Hossam L Kommy over 8 years

even if they were different It doesnt show who wins