My Rock, Paper, Scissors code says it works, but doesn't. How do I fix it?


#1

The activity will let me pass, but the code doesn't actually work for me. For example, if the game is a tie, it doesn't say so.

Here is my code:

var userChoice = prompt("Rock, paper or scissors?")

var computerChoice = Math.random()
console.log(computerChoice)
{
if (computerChoice < 0.33)
{console.log("rock");}
else if (computerChoice < 0.66)
{console.log("paper");}
else
{console.log("scissors");}
};

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 (choice1 === "scissors")

  {if (choice2 === "rock") {
        return "rock wins";
    }
    else {
        return "scissors wins";
    }}
};

compare (userChoice, computerChoice);

#2

If yuo log this, it will display the result.

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

#3

Thank you, but I've tried that too. The only difference that makes is whether there are quotes around it or not.

The problem I am having isn't whether it is printing out, it's that what it is giving me doesn't match the logic of rock paper scissors.

Another example would be I will choose rock, the computer will get scissors, but then it says paper wins.


#4

Here is the problem. computerChoice is not being set.

if (computerChoice <= 0.33) {
    computerChoice = "rock";
} else
if (computerChoice < 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

#5

That didn't work either. But I figured it out.

I am pretty sure the problem was that I didn't have choice1 and choice2 actually linked to anything.
I changed the initial userChoice and then made all of the computerChoice outcomes equal to choice2.
I set it up here so that it would print out a bunch of the information so I could fix the problem easier so there are some arbitrary console.log commands.

var choice1 = prompt("Rock, paper or scissors?")
console.log(choice1)
var computerChoice = Math.random()
console.log(computerChoice)
{
if (computerChoice < 0.33)
{choice2 = ("rock");}
else if (computerChoice < 0.67)
{choice2 = ("paper");}
else
{choice2 = ("scissors");}
};
console.log(choice2)

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 (choice1 === "scissors")

  {if (choice2 === "rock") {
        return "rock wins";
    }
    else {
        return "scissors wins";
    }}
};

(console.log (compare (choice1, choice2)));

#6

The example had a typo in it. Fixed now. That is what the instructions ask for. choice2 is a local variable in the compare function. computerChoice is the globally defined variable.


#7

Supplemental

If you're at all mathematically inclined then probabilities will interest you. This is a probability problem with nine outcomes. Where n = number of choices for each competitor, the probability of a win is 1 / (n*n) and there are n squared possible outcomes.

The 1 / n aspect is most interesting, since that is the control in a conditional expression for this overall outcome.

Let c = computerChoice

for this example.

    c = computerChoice
    if (c < 1/3) {
        c = 'rock';
    } else
    if (c < 2/3) {
        c ='paper';
    } else {
        c = 'scissors';
    }

    computerChoice = c;