What is wrong with my code?


#1



I get the error message "Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper".
It does this for all entries.


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


#2

your last else "paper wins' statement should go beneath your first if statement:

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

In place of that last else statement, put another else statement, nbut insead of returning "paper wins", return "scissors wins"

else{ 
            return "scissors wins";
        }

#3

ok, i've done that, but now shows me an error message "Oops, try again. Make sure to call your compare function with userChoice and computerChoice as the arguments!" what does it mean when it says "as the arguments"?


#4

you need to call userChoice(); and computerChoice(); at the end of your code.


#5

I don't quite understand the concept of 'calling'. how would that look as a part of my code. I keep getting the error message to check my syntax when i try to insert userChoice(); and computerChoice();


#6

You "call" functions like this:

userChoice();
computerChoice();

Click the link to learn more.
https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/0/3?curriculum_id=506324b3a7dffd00020bf661


#7

I had the same issue as the OP but the advice on here is wrong. You shouldn't be calling userChoice() or computerChoice().They're clearly variables not functions. My fault was incorrect return values. The error message wasn't really that helpful. There was nothing undefined just the wrong returned string.
Here's mine for what it's worth.

 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);
console.log("User: " + userChoice);
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 (choice2 === "rock") {
            return "rock wins";
        } else {
            return "scissors wins";
        }
    }
}
compare(userChoice, computerChoice);