Help debugging 8/9 - only returns on ties


#1

Text only returns on ties. The computer still returns rock/paper/scissors, but unless it's a tie there is no losing/winning dialogue. It says I got it correct but I am frustrated at this detail.

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

}}
compare (userChoice, computerChoice);


#2

How did you post your code with the formatting? I was going to post my completed code but it was too hard to read without formatting.


#3
  1. select the code after pasting here and push ctrl +k
  2. or you can push the </> in the tools pane
  3. for syntax highlighting all you have to do is put three backquotes at the beginning and end of your code

#4

Here's what I had. Thanks rydan!

   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! Smell what I'm cookin'";
            }
            else {
                return "Scissors wins!";
            }
        }
    }
    compare(userChoice, computerChoice);

#5

Hi! It seems to me that
- you've used 'if' function instead of 'else if' when choice1 is paper or scissors, that kind of 'nested' all possibilities inside the first 'else if' (when choice1 is rock) --- and don't forget the }s to close everything (I forgot it during my first 10 attempts or so... It was real fun to find out... :sweat_smile: )
- also at the last comparison you have two 'if's instead of 'if' and 'else', which might be problem as well.

At the end (after the 'if' for the tie) it should look something like this:

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";
        }
    } /*closing the last else if section*/
};  /*closing the whole compare function*/

And then the 'compare' should kill it...
Best of luck!