How do you make the set the variables again when it's a tie? 9


#1

Here is my code, for some reason it doesn't seem to work when it's a tie. It asks what the user's new answer is, but after the prompt nothing happens. Please help.

var userChoice = prompt("Do you choose rock, paper, scissors,lizard or Spock?");
if(userChoice.substring(0,4) === "rock"){
userChoice = "rock"
}else if(userChoice.substring(0,5) === "paper"){
userChoice = "paper";
}else if(userChoice.substring(0,8) === "scissors"){
userChoice = "scissors";
}else if(userChoice.substring(0,4) === "lizard"){
userChoice = "lizard";
}else if(userChoice.substring(0,5) === "Spock"){
userChoice = "spock";
}else{
userChoice = prompt("You need to write rock, paper, scissors,lizard or Spock dude.");
};
console.log("You choose " + userChoice + ".");

var computerChoice = Math.random();
if (computerChoice < 0.20) {
computerChoice = "rock";
} else if(computerChoice < 0.40) {
computerChoice = "paper";
} else if(computerChoice < 0.60){
computerChoice = "scissors";
} else if(computerChoice < 0.80){
computerChoice = "lizard";
}else{
computerChoice = "Spock";
};

console.log("AI chooses " + computerChoice + ".");

var compare = function(choice1,choice2){
if(choice1 === choice2){
console.log("The result is a tie! Go on and make a new choice.");
newUserChoice = prompt("What's your new choice. Type only 'rock', 'paper', 'scissors', 'lizard' or 'spock'.");
var newComputerChoice = Math.random();
if (newComputerChoice < 0.20) {
newComputerChoice = "rock";
} else if(newComputerChoice < 0.40) {
newComputerChoice = "paper";
} else if(newComputerChoice < 0.60){
newComputerChoice = "scissors";
} else if(newComputerChoice < 0.80){
newComputerChoice = "lizard";
}else{
newComputerChoice = "Spock";
}
newUserChoice = choice1
newComputerChoice = choice2
}else if(choice1 === "rock"){
if(choice2 === "scissors"){
return "Rock wins.";
}else if (choice2 === "paper"){
return "Paper wins.";
}else if (choice2 === "lizard"){
return "Rock wins.";
}else{
return "Spock wins.";
}
}else if(choice1 === "paper"){
if(choice2 === "scissors"){
return "Scissors wins.";
}else if (choice2 === "rock"){
return "Paper wins.";
}else if (choice2 === "lizard"){
return "Lizard wins.";
}else{
return "Paper wins.";
}
}else if(choice1 === "scissors"){
if(choice2 === "rock"){
return "Rock wins.";
}else if (choice2 === "paper"){
return "Scissors wins.";
}else if (choice2 === "lizard"){
return "Scissors wins.";
}else{
return "Spock wins.";
}
}else if(choice1 === "lizard"){
if(choice2 === "scissors"){
return "Scissors wins.";
}else if (choice2 === "rock"){
return "Rock wins.";
}else if (choice2 === "paper"){
return "Lizard wins.";
}else{
return "Lizard wins.";
}
}else if(choice1 === "spock"){
if(choice2 === "scissors"){
return "Spock wins.";
}else if (choice2 === "paper"){
return "Paper wins.";
}else if (choice2 === "rock"){
return "Spock wins.";
}else{
return "Lizard wins.";
}
}
};
compare(userChoice,computerChoice);


#2

Hello @boardmaster94002 ,
The code without indentation is not so easy to read but here is my 2 cents. Your code does not work when it is a tie because you have structured the game with if's and else if's. So in the function compare, if it is a tie then the first if will be executed and the program ends because if one of if's or else if's is executed the other else if's will not be executed. To overcome this problem you can use recursive function, which means you will have to call for your function in the if statement.So if it is a tie and the first if is executed you can make the function call itself again and the function will start over. So your first if should look like this, but even then you do not check if the user's input is entered correctly for a second time. So you can add the first part of the code after you ask for an input again.

var compare = function(choice1,choice2){
if(choice1 === choice2){
console.log("The result is a tie! Go on and make a new choice.");
newUserChoice = prompt("What's your new choice. Type only 'rock', 'paper', 'scissors', 'lizard' or 'spock'.");
var newComputerChoice = Math.random();
if (newComputerChoice < 0.20) {
newComputerChoice = "rock";
} else if(newComputerChoice < 0.40) {
newComputerChoice = "paper";
} else if(newComputerChoice < 0.60){
newComputerChoice = "scissors";
} else if(newComputerChoice < 0.80){
newComputerChoice = "lizard";
}else{
newComputerChoice = "Spock";
}
newUserChoice = choice1
newComputerChoice = choice2
compare(choice1, choice2)
}