9/9 addition: Asking players for another choice in case of draw


#1

Hi, I'm trying the addition stated in the title, trying to repeat the process if it's a tie. I thought if the computer's choice was turned into a function it would work but keep getting an error saying x isn't defined. First go at trying something different so this is rather frustrating. Thanks for any help. Code is:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerFunction = function(x){
    x = Math.random()
if (computerFunction < 0.34) {
	computerFunction = "rock";
} else if(computerFunction <= 0.67) {
	computerFunction = "paper";
} else {
	computerFunction = "scissors";
}
}
console.log("Computer: " + computerFunction(x));

var compare = function(choice1,choice2){
 if (choice1 === choice2) {
 return userChoice = prompt("Draw! Let's try again. Rock, paper or scissors?");
 return computerFunction;
 console.log(compare(userChoice,computerFunction));
 } 
 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"; }
    }
 else {
        return "Hey! That's not an option!";
    }
}

console.log(compare(userChoice,computerFunction(x)));

#2

I just copied and pasted everything before the compare() right after the "the result is a tie" and recalled the function compare. I had to change the returns to console.logs to get codecademy to display the winner after there was a tie. I saw some others wrapped the entire game in a function and recalled it. I did next steps 1 & 3. I did not add rope.

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

while (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors") {
    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) {
        console.log( "The result is a tie!"); 
        
        
        
    var userChoice2 = prompt("Do you choose rock, paper or scissors?");

    while (userChoice2 != "rock" && userChoice2 != "paper" && userChoice2 != "scissors") {
        userChoice2 = prompt("Do you choose rock, paper or scissors?");    
    }
    
        var computerChoice2 = Math.random();

        if (computerChoice2 < 0.34) {
	    computerChoice2 = "rock";
        } else if(computerChoice2 <= 0.67) {
	    computerChoice2 = "paper";
        } else {
	    computerChoice2 = "scissors";
        } console.log("Computer: " + computerChoice2);
    
        compare(userChoice2, computerChoice2);
        
          
        
        
    }
    else if (choice1 === "rock") {
        
        if(choice2 === "scissors"){
            console.log( "rock wins");
        }
            else
            {
                console.log("paper wins");
            }
    }
    else if (choice1 === "paper") {
        
        if (choice2 === "rock") {
            console.log("paper wins");
        }
        else 
        { 
            console.log("scissors wins");
        }
    }
 
    if(choice1 === "scissors") {
        if(choice2 === "rock") {
            console.log("rock wins");
        }
        else
        {
            console.log("scissors wins");
        }
    }
}

compare(userChoice, computerChoice)

#3

@larryy14
The idea of wrapping your choices in functions is pretty good but the implementation has some flaws:

var computerFunction = function(x){
    x = Math.random()
if (computerFunction < 0.34) {
	computerFunction = "rock";
} else if(computerFunction <= 0.67) {
	computerFunction = "paper";
} else {
	computerFunction = "scissors";
}
}

The parameter x of your function gets directly overwritten by Math.random, in other words it's never used and could therefore be deleted. The next thing is that this:

computerFunction = "rock";

overwrites your function with the value "rock" or "paper" or "scissors" (depending on x) but anyway this makes replaces your function and makes it therefore pretty useless. So either stick to the old way of choosing or think of something new.

As you directly use the value here:

console.log(compare(userChoice,computerFunction(x)));

you could for example use return instead of assigning the value e.g.

var computerFunction = function(){
    x = Math.random()
    if (computerFunction < 0.34) {
        return "rock";
    } else if(computerFunction <= 0.67) {
        return "paper";
    } else {
        return "scissors";
    }
 }

Now the function call would be

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

Btw the problem with the x is that x is a local variable to computerFunction so you cannot use it outside of it.

Last but not least to this:

return userChoice = prompt("Draw! Let's try again. Rock, paper or scissors?");
return computerFunction;
console.log(compare(userChoice,computerFunction));

return immediately exits the function so anything after return (here the other return and the console.log) will just be ignored. Also recursion (calling functions in functions) is ok to do the loop, but I would recommend to proceed until you know about loops as they are probably the better way to do this.


#4

@mquick66yahoo_com
Some ideas concerning your code. The "appropriate choice" while loop is pretty good but you may think of using a do while loop here to not repeat yourself:

var userChoice2 = prompt("Do you choose rock, paper or scissors?"); // almost the same

    while (userChoice2 != "rock" && userChoice2 != "paper" && userChoice2 != "scissors") {
        userChoice2 = prompt("Do you choose rock, paper or scissors?");    //as this
    }

also using a function to shorten the choice as @larryy14 did could make your code shorter and more readable. Last but not least why do so many people try to fix this tie problem inside of the compare function? Wouldn't it be much easier to wrap the function call in a while loop and react if the returned value is equal to "the result is a tie!"?


#5

Thanks both. I wanted to avoid any loops until they're covered in the tutorials a bit later on. Will come back to it once loops are introduced. Cheers for sharing your idea @mquick66yahoo_com and for the for the guidance @haxor789.


#6

@haxor789 Haxor789 Thank you for the criticism. I am very new at this. After reading your comments I learned from them and went back and cleaned up my code. I removed the "in case of a tie" part from the compare() and just wrapped the entire thing in a function and just called it again if there is a tie. I also cleaned up my while loop to be shorter than a do-while loop. I really appreciate your comments and it helped me learn. Thank you!

var loopy = function() {

var userChoice;

while (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors") {
userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();
}

//do {
//userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();
//} while (userChoice != "rock" && userChoice != "paper" && userChoice != "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) {
console.log( "The result is a tie!");
loopy();
}
else if (choice1 === "rock") {

    if(choice2 === "scissors"){
        console.log( "rock wins");

    }
        else
        {
            console.log("paper wins");

        }
}
else if (choice1 === "paper") {

    if (choice2 === "rock") {
        console.log("paper wins");

    }
    else 
    { 
        console.log("scissors wins");

    }
}

if(choice1 === "scissors") {
    if(choice2 === "rock") {
        console.log("rock wins");

    }
    else
    {
        console.log("scissors wins");

    }
}

}

compare(userChoice, computerChoice)
}

loopy();