9/9 If tie, how to restart the process?


#1

Hi,

I am really interested in knowing how to restart the process in case of tie or wrong entry.

Using a solution found in another answer, I wrapped all my code inside an initial function. When I run everything, I don't any error message but the process stil doesn't restart and I stay with my "This is not a correct entry, try again!" message.

What did I do wrong ?

My code :

      var game = function() {

	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! Try again");
	        game();
	    }
	    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 wins");
	        }
	    }
	    else {
	        return("This is not a correct entry. Try again!");
	        game();       
	    }
	};

	console.log(compare(userChoice, computerChoice));
};

game();

Thanks !


#2

@mitsudomoe Take a look at this post get an idea of how to do that
Click here to post