Rock, paper, scissors help please


#1

Guys, iv been going days trying to find a solution to this issue. I have tried everything i could imagine, but it still wont work.

When i type down this code:

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

and hit enter, the lesson 8 will congratulate me and move forward, but, my issue is, why doesnt the game console log the winner?
When i type rock, paper or scissors, it will log the computer choice and nothing else. It wont tell me whos choice won.
So my question is, how can i request the computer to log he winner?


#2

Well call the compare function.

compare(arg1,arg2)


#3

thanks for the fast reply, but its still not working.

If i add the compare (choice1, choice2) at the end of the function, it gives me "ReferenceError: choice1 is not defined" error. If i put it in the last } it just does not show.

See below my test:

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 "Tie";
    compare(choice1, choice2);
}

and

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 "Tie";
    }
compare(choice1, choice2);

#4

Maybe you should go back and review the function lesson. As far as I can see you already had a working function in the first post, where all you needed to do was to give it a starting command by adding a function call to the bottom of your program, right? Do you know how to call a function and how parameters and arguments work?

PS:

If i add the compare (choice1, choice2) at the end of the function, it
gives me "ReferenceError: choice1 is not defined" error.

Well that is correct, choice1 and choice2 are local variables to your function so using them outside of this context gives you a Reference Error. But you don't need choice1 and choice2 here but instead could use any value like "rock", "paper" or even better userChoice, computerChoice.


#5

thanks for the great help. Finally i got it to work.

Below is the revised script:

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

#6

It's probably not doing to do it on these tests because we are not that fat alone yet, run that same code on koding. com or something