Topic 8 choice 1 is scissors: Your Code returned 'paper' instead of 'undefined'


#1


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

When I run the code my console display is:
Computer: scissors
"scissors wins"

The error message reads: "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);


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

Replace this line with your code.


#2

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

who will win if inputs are scissors and paper ?


#3

Hey rcodeman,

Thanks for your response. It was a killer hint that led to the solution:
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);

//choice1 = user
//choice2 = cpu
var compare = function (choice1, choice2)
{
var user = choice1;
var cpu = choice2;
if (user === cpu) {return "The result is a tie!";}

else if (user==="rock")                {
    if(cpu === "scissors") {return console.log("User wins: " + user);}
    else  {return "The winner is: "  ;}        }

    else if(user === "paper")           {
        if(cpu === "rock"){return console.log("User wins: " + user);}
        else {return "scissors wins";}  }

        else if(user === "scissors")    {   
            if(cpu === "rock"){return console.log("Computer wins: " + computerChoice);}
            else{return "paper wins";}  }

};
compare(userChoice,computerChoice);


#4

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