Rock,Paper and Scissors Game: Both choices are the same! Please find the bug


#1



https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/1?curriculum_id=506324b3a7dffd00020bf661#


Oops, try again. Your compare function does not return 'The result is a tie!' when there is a tie.

Please help me find the bug.


As shown in screenshot, it outputs the tie result but still shows the error.


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 Choice: " + computerChoice);

var compare = function(choice1, choice2){
    if (choice1 === choice2){
        return "The result is tie.";
    }
    else if(choice1 === "rock") {
        if (choice2 === "scissors"){
            return "Rock wins";
        }
        else{
            return "Paper wins";
        }
    }
    else if (choice1 === "scissors"){
        if (choice2 === "rock"){
            return "Rock wins";
        }
        else{
            return "Paper wins"
        }
    }
    else if (choice1 === "paper"){
        if(choice2 === "rock"){
            return "Paper wins";
        }
        else{
            return "Scissors wins";
        }
    }
    else{
        return "You entered a wrong choice " ;
    }
};
console.log(compare(userChoice,computerChoice));


#2

@nabinjaiswal: Your code works and prints out the correct output for a given input but the things your message when it's a tie it's not equal to the ones you were supposed to print.
According to the instructions:


You forgot to write the "a" so it would be just the same as the instruction.
Regardless if your code works perfectly, not following the instructions won't let you pass.

Hope it helps!


#3

Thank you ! It help me out.
That was really a silly mistake.


#4

Glad I could help :slight_smile: