I cant figure out what im doing wrong


#1


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


Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper


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("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")
         }
         
         
     }
}

compare(userChoice, computerChoice)


#2

I think part of the problem is when you are defining your else if statement, you say is less than or equal to .67, if it was .27 for example, it would fall into both categories, rock and paper. What I did was say else if(computer choice === .34, .66). The comma signals, through, like a hyphen would in our english syntax. Additionally, I would change the iff statement to be less than .33 so that the two don't interfere with each other.
I don't know if this will solve your problem, but it might help. Everything else looks really good!


#3

Is this what is actually happening? If Math.random() yields 0.55, why would both the if statement and else if statement have an equal say in assigning a value to computerChoice?


#4

If it was .55 it would not, but what if it was .25 or .18? I'm not entirely sure that this is what's creating the error message, that's just what I saw and it's also what I did differently in my code. How did you construct yours?


#5

My if/else if/else statement for computerChoice was identical. I think @aodhan1's issue is addressed in the error message. If your inputs are scissors and rock, why would paper win???

@aodhan1, you should look at all the else if statements in your compare function. Make sure the correct result is returned according to the rules of the game "rock, paper, scissors".

Rock breaks scissors.
Paper covers rock.
Scissors cut paper.

So if choice1 is rock and choice2 is scissors, paper cannot win if it wasn't an input.


#6

@biff75 is right...

Inside of your else if statement concerning rock, you say that if rock goes against scissors, then paper wins...
I think all you have to do it change paper wins to rock wins, and make sure the rest of your rules are correct. :slight_smile:


#7

thank you so much, i didn't notice that


#8

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