Build "Rock, Paper, Scissors", what am I doing wrong?


#1


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


Hi! I have a problem with this code. It doesn't send the error message, I can pass to another lesson, but it doesn't work correctly.
The computer.log is showing me:

Computer: scissors
undefined

instead of, for example: "rock wins"

I cannot find the problem.


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 {
            return "scissors wins"; }
        }
      }
      var compare = function(choice1, choice2)
{
if(choice1 === choice2)
{ 
    return "The result is a tie!"; } 

      else if(choice1 === "paper")
        {
            if(choice2 === "rock")
        { return "paper wins"; }
        else {
            return "scissors wins"; }
        }
      }
      var compare = function(choice1, choice2)
{
if(choice1 === choice2)
{ 
    return "The result is a tie!"; } 

      else if(choice1 === "rock")
        {
            if(choice2 === "paper")
        { return "rock wins"; }
        else {
            return "scissors wins"; }
        }
      }
      console.log(compare(userChoice, computerChoice));


#2

Hi there

Okay, so I've had a quick look and can immediately see that your code is quite fragmented. You have three separate compare functions. We only need one; having more with the same name can produce unexpected results.

I don't want to give it all away because I know you can solve it. But here's the basic outline for how you might want to structure the compare function.

var compare = function(user, computer) {
    
    if() // result is a tie
    
    else if() // user is rock
        if() // compare user with possible computer results ie paper/ scissors
        else
       
    else if() // user is paper
        if() // compare user with possible computer results ie rock/ scissors
        else
        
    else if() // user is scissors
        if() // compare user with possible computer results ie rock/ paper
        else
    
}

Hopefully, that's enough of a hint. I suggest starting from scratch and really nailing down the structure/ syntax.
If you're still stuck don't hesitate to ask for more help :slight_smile:


#3

Hi!

Thank you for your help!!
I erased compare funcions but it still didn't worked as it should.
Then I did this (see the code below). I just changed === to =.
I don't understand though, why it didn't work with ===, in the lesson that was the correct form of the code... Could you explain me that please?

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

#4

Glad to help.

We use === when we want to compare two values. A single = is used when we want to assign a value to something. So in your cases above you're re-assigning strings to choice1 and choice2 when you should be comparing the strings instead. So switch those back to === like you had previous; and then you'll be good to go.

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

Here's another mistake you may have accidentally made. We split parameter values by adding a comma in between them not a plus sign. In this case you'd be joining for example(as a test case): rock+rock to make rockrock then passing it off for comparing. Obviously that's not what we intend to do.

After those changes all should be well except I noticed a logical error in your code as well. Your third else if statement. Commented in the relevant places to show you what's wrong.

else if(choice1 === "rock")
{
   if(choice2 === "paper")
   { 
      return "rock wins"; // rock vs paper = rock wins? Should be paper wins
   }
   else 
   {
      return "scissors wins"; // Rock vs scissors = scissors win? Should be rock wins
   }
}

Just double check in case I missed some more logical errors. That was the only one that caught my attention.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.