How to tell the program to disregard any answers other than what i want


#1

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){
console.log ("The result is a tie!");
prompt("Let's play till someone wins. Which do you chooce this time, rock, paper or scissors?")
}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 === "paper"){
return "scissors WINS";
}else{
return "rock WINS";
};
};
};

compare(userChoice,computerChoice);

Above is the code that i used in the program and it worked perfectly. I figured out a way to add a little extra to the task as well by making it play again if there was a tie which i really liked. In this code the user needs to make a choice out of rock, paper and scissors to play however if they make a typo or put in something incorrect the program just lets them. It does not play the game but it just ignores it.

So my question is, is there a way to make it so the computer will disregard all answers other than rock, paper or scissors?

Any help would be greatly appreciated.

Thanks


#2

@byteninja39979 try this

var userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();
var computerChoice = Math.random();
var tie =  true;

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){
    
                //try not do such things like prompting
                //in your method
                //handle it outside the method
               if(choice1 === choice2){
                    return ("tie"); // had to modify this string a bit
                }
                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 === "paper"){return "scissors WINS";}
                    else{return "rock WINS";}
                }
};

//==========================================================================//
//                                                                         //
//                              CHOOSE ONE OF THE OPTIONS BELOW            //
//                                                                         //
//=========================================================================//

//=====================option 1 using arrays=================================
var options = ['rock', 'paper','scissors'];
while(options.indexOf(userChoice)== -1 || compare(userChoice, computerChoice) == "tie"){
   computerChoice = Math.random();
   userChoice = prompt("Let's play till someone wins. Which do you chooce this time, rock, paper or scissors?");
}

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

//======================option 2 using regex ================================
//===You can alsouse regular expressions in this case you do this============
//define input format
var regex = /^[A-Za-z]+$/;
while(!userChoice.match(regex)|| compare(userChoice, computerChoice) == "tie"){
       computerChoice = Math.random();
       userChoice = prompt("Let's play till someone wins. Which do you chooce this time, rock, paper or scissors?"); 
}
console.log(compare(userChoice, computerChoice));


//======================option 3 writing handlers sepearately=====================
// if you wanted to implement the input validation handler separately 
//then do this
while(options.indexOf(userChoice)== -1 ){ // or this (!userChoice.match(regex)) for the condition
    userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();  
}

// then after handle when the compare returns a tie
while(compare(userChoice, computerChoice) == "tie"){
       computerChoice = Math.random();
       userChoice = prompt("Let's play till someone wins. Which do you chooce this time, rock, paper or scissors?"); 
}
console.log(compare(userChoice, computerChoice));

or you can test it here
Click here to go to labs


What if a user makes an inappropriate choice like 'dog'?
9/9 3 objective
9/9 If tie, how to restart the process?
9 - Create your own game
Unexpected number error
9. NEXT STEP (looping untill the user types in the right choice)
#3

Here's my question: I too am having issues trying to figure this out. But aren't we only supposed to be using what we've learned up to now? The while loop is great but it hasn't been taught yet. So how do I complete this without skipping ahead? I'm assuming the answer must be more simple than this.


#5

Here's mine, does nothing but return user input at the second prompt screen in 'var compare'.

Any suggestions without going out of the scope of what I've learned?

var computerChoice = Math.random();
if (computerChoice < 0.25) {
	computerChoice = "rock";
} else if(computerChoice <= 0.50) {
	computerChoice = "paper";
} else if (computerChoice <= 0.75) {
    computerChoice = "rope";
} else {
	computerChoice = "scissors";
} 
console.log("Computer: " + computerChoice);
console.log("You: " + userChoice);

var compare = function(choice1, choice2){
    if (choice1 === choice2){
        return "It's a tie, choose again!" + prompt(userChoice);
    } else if (choice1 === "rock"){
        if (choice2 === "scissors"){
            return "rock wins";
        } else if(choice2 === "rope"){
            return "rope wins";
        } else {
            return "paper wins";
        }
    } else if (choice1 === "paper"){
        if (choice2 === "rock"){
            return "paper wins";
        } else if(choice2 === "rope"){
            return "rope wins";
        } else {
            return "scissors wins";
        }
    } else if (choice1 === "rope"){
        if (choice2 === "rock"){
            return "rope wins";
        } else if (choice2 === "paper"){
            return "rope wins";
        } else {
            return "scissors wins";
        }
    } else if (choice1 === "scissors"){
        if (choice2 === "rock"){
            return "rock wins";
        } else if(choice2 === "rope"){
            return "scissors wins";
        } else {
            return "scissors wins";
        }
    } else if (choice1 !== "rock"){
        if (choice1 !== "paper"){
            if (choice1 !== "scissors"){
                return "You didn't enter a valid response!";
            }
        }
    }
}

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