Getting my game to replay


#1


I'm not stuck on a specific lesson, I am just trying to add on to my finished rock, paper, scissors game.

I replaced the return "The result is a tie." with new code (shown below). When the computer and the user tie the confirm popup comes up and the prompt popup also comes up. My problem is getting the computer to make another choice and compare it to the user's choice again.

(Also if there is a way to make the code stop running if someone clicks cancel on the confirm popup I would be greatful to hear it...)


if (choice1===choice2) {
       if (confirm("The result is a tie! Want to break the tie?")){ prompt("Do you choose rock, paper or scissors?");
       }
        }


#2

@daisy_of_doom,

1 To extend the userChoice-prompting with a validity-check, you would
embed the prompting into a function-body...
So you would start by
var getUserChoice = function() { your prompting here };

2 If you are extending the choice-possibillity, you have to extend the
so-called truth-table.

Your new Decisions-Table: fill-in ?? wins...

              | ch-2  |  ch-2  | ch-2     | ch-2   |
              | rock  |  paper | scissors | rope   |
  ------------|-------|--------|----------|--------|
              |       | paper  |  rock    | ????   |
 choice1  rock|   X   | wins   |  wins    |  wins  | (1st else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              | paper |        |scissors  |  ? ?   |
 choice1 paper| wins  |    X   |  wins    |  wins  | (2nd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  rock |scissors|          |  ?  ?  | 
 ch-1 scissors|  wins | wins   |   X      |  wins  | (3rd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  ? ?  | ? ? ?  |  ? ? ?   |        | 
 ch-1 rope    |  wins | wins   |   wins   |    X   | (4th else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|

3 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 ( allready done !!)

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 getUserChoice = function () {
 var userChoice;
 do {
      userChoice = prompt("Choose either rock, paper or scissors","paper");
     } while (userChoice !== "rock" &&
          userChoice !== "paper" &&
          userChoice !== "scissors");
     // console.log("UserChoice is 1: "+ userChoice);
     return userChoice;
};
var getComputerChoice = function() {
var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if(computerChoice <= 0.67) {
	computerChoice = "paper";
  } else {
	computerChoice = "scissors";
  }
  //console.log("computerChoice 1: " + computerChoice);
  return computerChoice;
};

//var userChoice = getUserChoice();
//var computerChoice = getComputerChoice();

//=================================

var compare = function(choice1, choice2){ ///opens the function
  if(choice1 === choice2){ ///opens the if
      return("The result is a tie!");
  } ///closes the if

  else if (choice1 === "rock"){ ///opens the else if
      if(choice2 === "scissors"){ ///opens the if
          return "rock wins";
      }  ///closes the if
      else{ ///opens the else
          return "paper wins";
      } ///closes the else
  } ///closes the else if

  else if (choice1 === "paper") { ///opens the else if
      if (choice2 === "rock") { ///opens the if
          return "paper wins";
      } ///closes the if
      else { ///opens the else
          return "scissors wins";
      } ///closes the else
  } ///closes the else if

  else if (choice1 === "scissors") { ///opens the else if
      if (choice2 === "rock") { ///opens the if
          return("rock wins");
      } ///closes the if
      else { ///opens the else
          return("scissors wins");
      } ///closes the else
  } ///closes the else if

}; ///closes the function

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

var playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";
    while (playAgain) {
    //execute the pre-declared getUserChoice-function
    userChoice = getUserChoice();
    console.log("User choice is " + userChoice);

```
        //execute the pre-declared getComputerChoice-function
        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!") {
        //    console.log("It is a tie!");
        if (theResult === "The result is a tie!") {
            //console.log("The result is a tie!");
            playAgain = true;
            console.log("==== Play once more =====");
        } else {
            playAgain = false;
            console.log("==== End of Game =====");
        }

    //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();