9. Next Step {HELP}


#1

Hello
What if a user makes an inappropriate choice like 'dog'? How can we extend the function to handle that?

What if players in the game could also choose Rope in this game?

In this version, if both players make the same choice, the game returns a tie. What if the game didn't end there but instead asked both players for new choices?

How can I do this all ? I really do not how to add this on my game. But I want them.
Help.

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} 
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
 if (choice1 === choice2) {
     return "The result is a tie!";
 } else if(choice1 === "rock") {
    if (choice2 === "scissors") {
        return "Rock Wins";
    } else {
        return "Paper Wins";
    }
 } else if(choice1 === "paper") {
    if (choice2 === "rock") {
        return "Paper Wins";
    } else {
        return "Scissors Wins";
    }
 } else if(choice1 === "scissors") {
    if (choice2 === "rock") {
        return "Rock Wins"
    } else {
        return "Scissors Win"
    }
 }
};
compare(userChoice, computerChoice);

That is my code. Advanced Thanks ! :smiley:


#2

@csssurfer42257,

What you want is a loop-construct...
create the getUserChoice function using return-statement
create the getComputerChoice function using return-statement
creat the compare function using return-statement

As you are using return-statements you call the compare-function
var theResult = compare(userChoice,computerChoice);
and thus having the =result= of the game in the theResult variable

===========================================

var playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";

   while (playAgain) {

       //execute the pre-declared getUserChoice-function
       var userChoice = getUserChoice();
       console.log("User choice is " + userChoice);

        //execute the pre-declared getComputerChoice-function
        var computerChoice = getComputerChoice();
        console.log("Computer choice is " + computerChoice);

        //execute the compare-function and capture the-result
        theResult = compare(userChoice,computerChoice);
        console.log("The result is " + theResult);

    if (theResult === "It is a tie!") {
       playAgain = true;
    } else {
      playAgain = false;
    }

    //end of -while- loop is reached, and if VALUE of playAgain === true
    // the loop will start over
    } //closing the while-loop
 }; //closing the function-body of playTheGame

// call the function playTheGame
playTheGame();