Next steps Question Help!


""What if a user makes an inappropriate choice like 'dog'? How can we extend the function to handle that?""
I think it's not enough by using just if/else statement,could anyone come up with other ways to accomplish the requirement? I want to use do/while function to ask user to choose again until the input is right but i don't know how to use it.

var userChoice = prompt("Do you choose rock, paper or scissors?");
do{userChoice=prompt("Please choose again!!")}
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){
      {return "The result is a tie!"}
      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 "scissors wins"}
compare(userChoice, computerChoice);


do while loops aren't covered are they? You could do something like:

do {
  var userChoice = prompt("Do you choose rock, paper or scissors?");
} while(userChoice != "rock" && and userChoice != "paper" && userChoice != "scissors")

the loop runs as long as the condition is true, if the user enters something invalid, all conditions are evaluated to true (since they do not equal a valid choice), if the user enters something valid, one condition is evaluated to false (since it does equal a valid choice), false && true && true is false, so the loops stops, as it should, since the user enters something valid


@stetim94 I think I GOT IT!! Thank you so much!


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