Can any one help me to debug this code?Question Build "Rock, Paper, Scissors" 8/9


#1

Below code is giving me error while executing.

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 "rocks 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 === "paper")
    {
     return "paper wins";   
    }
    else
    {
        return "rock wins";
}

}

};

console.log ("The challenger has chosen" + " " + userChoice);
compare (userChoice, computerChoice);

#2

What is the error?

Just from reading this, it looks like the error could be where choice1 = scissors and choice2 = paper It should return "scissors wins" not "paper wins".


#3

Dude, this is so weird...

At first, I was getting errors, too, with your code here -- but then I tried this same code of yours over at http://www.webtoolkitonline.com/javascript-tester.html and it seemed to have worked fine there!!

Even weirder...I then tried your code in another browser -- in Firefox (I was using Chrome originally) -- and it worked there, too!!!!!

I rechecked in Chrome and your code was still giving me errors, but after some time passed...your code works fine everywhere!!!!!!!!

Too friggin' weird...so it looks like there's nothing wrong with your code ('cept it could use more indentation!): congrats!


#4

I have done changes in my code as you said and now its working fine, thank you @jojo_0

Thank You @dc_super_rich for sharing the link and debugging the code. I have make change according to what @jojo_0 said and now my code is working fine.