Build "Rock, Paper, Scissors" 8/9 What am I doing worng?


#1

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

};

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


#2

Hi @ravidgal,

At a quick glance, and just running some simple tests, your code is solid, the only guess I will take before I ask you to please post what error you are getting, or what is occurring you believe should not, is that maybe you are entering the value for userChoice variable (the prompt box) with upper case and so when your code goes to evaluate it comes out unexpected?

If this is so, go ahead and swap out your first line for the following, if not please do paste what error you are getting :smile:

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

Just as an additional note, what this will do is convert your prompt entry to all lower case, in this way you uniform your input and you ensure you get a match when you compare it in your code :wink:


#3

Hey @ravidgal,

Not any big problems in your code, just the bottom part here:

You have to use a variable to use the compare function with userChoice and computerChoice, and you have to put in order userChoice then computerChoice.

For example:

var result = compare(userChoice,computerChoice)

console.log(result)

This works. :smile:

You see what I did there?


Syntax Error
#4

Oops, try again. Make sure to call your compare function with userChoice and computerChoice as the arguments!