Getting an undefined message


#1


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


Hey guys, maybe I'm missing something here but although I'm getting a success message I'm getting an undefined error message. Any help would be appreciated.

This is what I get in the console:
Computer: paper
undefined


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!" //This is a tie
      
  } else if (choice1 === "rock") {      
      if(choice2 === "scissors") {
      return "rock wins" //If P1 chooses Rock and P2 chooses scissors
      } else {
            return "paper wins" ;
        }
  
  } else if (choice1 === "paper") {
      if (choice2 === "rock")     {
       return "paper wins"   //If P1 chooses Paper and P2 chooses Rock
      } else {
            return "scissors wins" ;
        }
      
  } else if (choice1 === "scissors") {
      if (choice2 === "paper")     {
       return "scissors wins"   //If P1 chooses scissors and P2 chooses paper
      } else {
            return "rock wins" ;
        }
  
  
  }
}

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


#2

its returning undefined whenever you type anything that is not the strings : 'rock' , 'paper' or 'scissors' into the prompt .

it is undefined because you havent told javascript what to do if a user types in some string that isnt what we set the game up for .


#3

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