End 4/9 - adding optional conditions to the game


#1

/* When a user inputs an answer not equal to rock, paper, or scissors, how do I correctly prompt them to re-answer? Right now the second prompt comes up even with a correct answer. Is there a way to perpetually re-prompt the user until they give a correct answer? */

var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice !== "paper","rock","scissors")
{ prompt("Please pick from rock, paper, or scissors");}


#2

At the end of my compare function (I mean right next to the end of the bracket) I put

return "WHAAAT?"

It works, and prints it out when you do not put rock, paper, or scissors.


#3

You can try this

var userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();

while(true){
    if(userChoice === "paper" ||userChoice === "scissors"||userChoice === "rock" ){
        break;
    }
    alert("Please pick from rock, paper, or scissors");// you can console.log() this part
    userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();
   
}
console.log("Your choice is : "+userChoice);

or you can use a do/while loop like this

var userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();

do{
    alert("Please pick from rock, paper, or scissors");//you can console.log() this part
    userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();
}while(!((userChoice === "paper" ||userChoice === "scissors"||userChoice === "rock" )));


console.log("Your choice is : "+userChoice);

#4

One possible way is to use the Array indexOf()-Method
= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
in combination with a while-loop
= https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Statements/while

var getUserChoice = function() {
   var possibleChoice = ["paper","rock","scissors"];
   var userChoice = "xxx";
   while(possibleChoice.indexOf(userChoice) === -1 ) {
     userChoice = prompt("Do you choose rock, paper or scissors?");
     console.log( possibleChoice.indexOf(userChoice) );
   }
   console.log("userChoice is : "+userChoice +
          " [index-nr in Array = "+
          possibleChoice.indexOf(userChoice)+"]" );
   return userChoice;
};
var userChoice = getUserChoice();

PS;

Reference: ( google search )

== the Book ==
[your question] site:developer.mozilla.org