If wrong input how to set label and continue for new input?


#1


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

gives error "undefined label start"


start:
 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!";
   break start;
 }
 else if(choice1==="rock")
 {
  if(choice2==="scissors")
  {
      return "rock win";
 }
 else{ return "paper win";}
 }
 
 else if(choice1==="paper")
 {
     if(choice2=="rock")
     {
         return "paper win";
     }
     else {return "scissors win";}
 }
 
 else if(choice1==="scissors")
 {
     if(choice2=="rock")
     {
         return "rock win";
     }
     else {return "scissors win";}
 }
 else{
     console.log(choice1 + " is not valid entry");
 }
};

var result = compare(userChoice,computerChoice)
console.log(result);


#2

I think you have else instead of else if
change your else to else if, and print within the parentheses choice1 !==("paper","rock","scissors"))

the compare is already a function you don't need to define it with a result variable, so you can print compare(userChoice,computerChoice)

it well look like this

.....
.....
     }
else{ return " scissors wins";
        }
    }
    else if(choice1 !==("paper","rock","scissors")){
        console.log(choice1 + " is not valid entry");
    }
}
compare(userChoice,computerChoice)

#3

thanx for reply ...i get your code very well but i want when user enter wrong input ...program gives error message and execution start from starting point ....so want to know how can i set label for this code ..i m new to javascript


#4

oh okay..
we can put the whole thing in a function named ourGame function ourGame() to start the game all over again if choice1=== choice2 or even if choice1 not exists ( not rock, paper or scissors)
in the last else if condition we changed in the last post we could put a confirm-window for the user to tell about the error. To restart the game again should the order ourGame() be printed instead of your break start code. Don't forget to change the code return to console.log

 if(choice1===choice2) {
     console.log( "The result is a tie!");
     ourGame();

and in the else if condition here

else if {(choice1 !==("paper","rock","scissors"))
     confirm(choice1+" "+ "is not valid entry")
     console.log(choice1 + " not exists");
     ourGame();

As the compare order is now in our function (ourGame) we should run it in a console.log or as you did in a result variable
var result = compare(userChoice,computerChoice)
console.log(result);

at the end and after closing the bracket we should normally print our order to run it

ourGame()

so will it look like

function ourGame(){
 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);
console.log( "You: " +userChoice)

var compare= function(choice1,choice2){
 if(choice1===choice2) {
     console.log( "The result is a tie!");
     ourGame();
 }
 else if(choice1==="rock")
 {
  if(choice2==="scissors")
  {
      return "rock win";
 }
 else{ return "paper win";}
 }
 
 else if(choice1==="paper")
 {
     if(choice2=="rock")
     {
         return "paper win";
     }
     else  {
         return "scissors win";}
 }
 
 else if(choice1==="scissors")
 {
     if(choice2=="rock")
     {
         return "rock win";
     }
     else {
         return "scissors win"}
 }
 else if (choice1 !==("paper","rock","scissors")){
     confirm(choice1+" "+ "is not valid entry")
     console.log(choice1 + " not exists");
     ourGame()
 }
};

var result = compare(userChoice,computerChoice)
console.log(result);
}
ourGame()

I hope this could help you


#5

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