"What if the game didn't end there but instead asked both players for new choices?"


#1


I am trying to extend the game by adding in an additional prompt for when there is a tie so that players can choose again in order to break the tie. I removed the "return The result is a tie!" part and just added a "prompt" but when using this method it does not return the string message as the original does. ( See screenshots to understand )


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 ) {
        prompt("The result is a tie! Choose again!"); <---- _/This is the part i changed to prompt users there was 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 win";
        }
    } else if (choice1 === "scissors") {
        
         if ( choice2 === "rock" ) {
            return "rock wins";
            
        } else {
            return "scissors win";
        }
    } else if (choice1 !== "rock","paper","scissors") {
        return "What a silly answer you idiot. I'm not playing anymore.";
    }
};

compare(userChoice,computerChoice);


#2

If it is a tie you need to:

  1. inform the user that it's a tie;
  2. ask user for the choice and save input in the userChoice variable;
  3. assign new random value to the computerChoice and later change this value to "rock", "paper" or "scissors";
  4. execute compare function with new values (use console.log around the function call to print the result to the console).

#3

I apologize ahead of time but i am new to programming. How would i go about coding those steps you mentioned ?


#4

Step 1 & 2. You just need to assign result of prompt("The result is a tie! Choose again!"); to the userChoice variable:

userChoice = prompt("The result is a tie! Choose again!");

Step 3. You basically have all the code you need. You don't have to define computerChoice variable, because it is already defined:

computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);

Step 4. You just need to call your function like at the end of the script, but it might be a good idea to put this code inside the console.log statement:

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

#5

Mhm followed all the steps as most of them were done for the most part and now getting "undefined".


#6

You should execute all the steps described by me when it is a tie.

I don't want to discourage you, but maybe it's not the right time to try this challenge? If you don't understand why you need to select a new value for computerChoice and call the function again... you are simply not ready and it does not make sense to follow instructions if you don't understand reasoning behind them.


#7

Not discouraged. Of course i want to know the reasoning behind the steps provided i am trying to make sense of it on my own from what i already know and the steps you provided but am unable to at the moment, because of my limited knowledge but that doesn't stop me from exploring what i don't know. Is there anywhere you can direct me to understand this issue more thoroughly ? Or if you don't mind just explaining the reasoning behind me having to create a new value for computerChoice ?


#8

Ok, let's say that you want to go to sleep, but computer wants you to watch one more episode of your favourite show. You decide to play rock, paper, scissors to solve this problem. Tie is not an option here, right?

You play rock and computer plays rock, it's a tie, so you guys have to play again, right? If computer is not able to choose again that's an easy win for you, you just need to choose paper and go to bed. That's unfair.

And that's exactly what you need to do. You have to get new value for userChoice and computerChoice and execute the compare function again.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.