Challenge 3. part 9 of rock/paper/scissors


#1


Hello everyone!

So I've just completed the rock/paper/scissors project and I'm working 
on improving the game. I managed to figure out how to make the game 
include rope and prevent answers that aren't "rock", "paper", "scissors" 
or "rope". However I'm struggling to work out how to ask the player for 
a new choice after a tie. 
I would really appreciate some hints :slight_smile:  

Anyway here's what I have so far:

var userChoice = prompt("Do you choose rock, paper, scissors or rope?")

if (userChoice === "rock") {
        userChoice = "rock";
}

else if (userChoice === "scissors") {
    userChoice = "scissors";
}

else if (userChoice === "paper") {
    userChoice = "paper";
}

else if (userChoice === "rope") {
    userChoice = "rope";
}

else {
    (userChoice = "didn't pick rock, paper, scissors or rope..");
}

console.log("user: " + userChoice);

var computerChoice = Math.random();
if (0 <= computerChoice <= 0.25) {
	computerChoice = "rock";
} else if (0.26 <= computerChoice <= 0.5) {
	computerChoice = "paper";
} else if (0.51 <= computerChoice <= 0.75){
	computerChoice = "scissors";
} else if (0.76 <= computerChoice <= 1){
    computerChoice = "rope";
} 
    

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 if (choice2 === "paper") {
            return "paper wins";}   
            
        else if (choice2 === "rope") {
            return "rope wins";}
}

    else if (choice1 === "paper") {
        
        if (choice2 === "rock") {
            return "paper wins";}    
        else if (choice2 === "scissors") {
            return "scissors wins";}  
        else if (choice2 === "rope") {
            return "rope wins";}
}

    else if (choice1 === "scissors") {
        
        if (choice2 === "rock") {
            return "rock wins";}
        else if (choice2 === "paper") {
            return "scissors wins";}    
        else if (choice2 === "rope")  {
            return "scissors wins";}
}

    else if (choice1 === "rope") {
        if (choice2 === "rock") {
            return "rope wins";}
        else if (choice2 === "paper") {
            return "rope wins";}
        else if (choice2 === "scissors") {
            return "scissors wins";
            }
}

};

compare(userChoice,computerChoice)


#2

Make your if/else statement after your var computerChoice = Math.random(); into a function like this
var computerPick = function() {
if (0 <= computerChoice <= 0.25) {
computerChoice = "rock";
} else if (0.26 <= computerChoice <= 0.5) {
computerChoice = "paper";
} else if (0.51 <= computerChoice <= 0.75){
computerChoice = "scissors";
} else if (0.76 <= computerChoice <= 1){
computerChoice = "rope";
} }

Then try adding this bit of code to your if statement inside of your compare function

confirm ("The result is a tie!");
        userChoice = prompt("Choose again! Rock, paper, scissors or rope?").toLowerCase();
        if (userChoice === "rock" || userChoice ==="paper" || userChoice === "scissors" || userChoice === "rope") {       
        computerChoice = Math.random();
        computerPick(); 
        compare(userChoice, computerChoice);
        }

#3

Have you noticed if you change it and put computerChoice first insead of userChoice example:
compare(computerChoice, userChoice) it changes it a bit... Do you know why? I'm trying to understand the reason.


#4

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