9. Next steps need help to finish it all :)


#1



Hi,

In the next step section I've managed to complete N°1 which tells what if a person doesn't enter the right value.. Now it ask again until you do.

But when there is a tie, I only manage to ask once more and not if there is two ties for example.. Does anyone know how to loop it back to the beginning? Should I create a function and just reask it after the console.log "this is a tie"?

Thank you guyzzzz

var valid = function(){
    var userChoice = prompt("Do you choose rock, paper or scissors?");
    if (userChoice === "rock")
        {
        return "rock"
        }
    else if (userChoice === "scissors")
        {
            return "scissors"
        }
    else if (userChoice === "paper")    
        {
            return "paper"
        }
        valid();
    }
    
userChoice = valid();
    
var computerChoice = Math.random();
if (computerChoice < 1/3) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2)
{
    if(choice1 === choice2) 
    {
       console.log("The result is a tie!");
  if(choice1 === choice2) {
  valid();
   }
    }
        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(choice2 ==="scissors")
        {
            return("scissors wins");
        }
    }
    else(choice1 === "scissors")
    {
        if(choice2 ==="rock")
        {
        return("rock wins");
        }
        else(choice2 ==="papers")
        {
            return("scissors wins");
        }
    
    }
    
}
compare(userChoice, computerChoice);

#2

Hey @digitalslayer27096,

for your valid()-function, refer to this.

To the next steps:
Do you want to ask the user, if he or she wants to restart the game AT ALL at the end, or just to make a new choice if it's a tie?


#3

Why use a valid(), I got mine to work by just adding an 'else' statement to the end of if(choice1 === choice2)
this way the computer will pick rock paper scissors and then if your response doesn't match it will skip all the code from function compare If and go to else statement that I have setup as "Invalid option"

The only thing I can't seem to figure out is the new game on tie, and new game prompt at the end of the game. if someone could post inline for a complete code on how they did this I would be grateful.

Thanks!


#4

I think I might understand now, because valid makes it a function instead of a variable which can be called again, I will try that out and see if it works.


#5

Still, using your valid, at the end it does not work perfectly as after retyping rock paper or scissors the console only prints out computerChoice but not who won.