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


#1


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


The error message is "Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and rock" Isn't this what the program should do???


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


#2

this bit off code:

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

and then specific your else clause, if else where to run, it would mean choice1 is scissors and choice2 is rock. How then does paper wins?


#3

Im getting the same exact error. I have been stuck on this for hours! Can a fresh set of eyes please tell me what im missing?

Thank you.

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){//1a
if(choice1 === choice2){//2a
return "The result is a tie!";
}//2b
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 "scissor wins";

}
}

}
console.log(compare(userChoice, computerChoice));


#4

here:

else if(choice1 === "scissors") {

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

}
}

you return scissor wins instead of scissorS wins


#6

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