I don't know how to finish the game with advanced conditions!


#1

My first Assignment: What if a user makes an inappropriate choice like 'dog'? How can we extend the function to handle that?
My second Assignment: 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?
My code:

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 wins";
        }
    }
    if(choice1 === choice2) {
        return userChoice = prompt("Do you choose rock, paper or scissors?");
    }
};
compare(userChoice, computerChoice);

My thinking: I just added this part of code

if(choice1 === choice2) {
        return userChoice = prompt("Do you choose rock, paper or scissors?");
    }

because if I put it in the end I thought that the parameters will not work. Now it's giving me on a draw that 'The computer wins'.
And for the second topic of this question I have no idea how to restrict recognision of just 3 words: "rock", "scissors", "paper". Thx you in advance and have a nice weekend!


#2

@neagumihai54321gmail,

Do not tamper with the functionality of the compare() function !!

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

#3

Pretty tricky. So my second if statement must be nested inside the while loop?. And, finnaly, I must change compare(userChoice, computerChoice); with playTheGame();?
Oh... and nesting all my code in var playTheGame =function() { my code }?


#4

Do you know about loops especially while and do/while loops yet?
If no I'd say you're better of going on and review it later as it really makes more sense to give it a try when you know about loops. Also I think the compare function does a pretty solid job and and shouldn't change the function but instead use it's output to achieve what you want to do :wink: