This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Build "Rock, Paper, Scissors" Forum

View Course

592 points
68e1a51ff1bf6029e2b2cc29eaeb4176?s=140&d=retro
Submitted by
Christian Pelagio
about 3 years ago

Terminating the program if the user did not type ROCK PAPER SCISSORS

Good day guys

What I want is whenever the user did not type ROCK PAPER SCISSORS the program will automatically end and will not continue to var Computer.

Thanks for your help Codeacademy ROCKS!!!!

Here is my code:

confirm("Welcome!!!");
var userChoice=function(player1){
var waiting=("Computer's Turn....");
if (player1==="ROCK"){
confirm (waiting);
userChoice=1;
console.log("You are"+" "+player1);
}
else if (player1==="PAPER"){
confirm (waiting);
userChoice=2;
console.log("You are"+" "+player1);
}
else if (player1==="SCISSORS"){
confirm (waiting);
userChoice=3;
console.log("You are"+" "+player1);
}
else{
confirm ("Please select from the choices!");
}
};
userChoice (prompt("CHOOSE:ROCK,PAPER or SCISSORS"));

var computerChoice=function(computer){
if(computer>=0.67){
confirm("Computer Selected:SCISSORS");
computerChoice=3;
console.log("Computer is SCISSORS");
}
else if(computer>0.333){
confirm("Computer Selected:PAPER");
computerChoice=2;
console.log("Computer is PAPER");
}
else{
confirm("Computer Selected:ROCK");
computerChoice=1;
console.log("Computer is ROCK");
}
};
computerChoice (Math.random());

var Result=function(result){
if (result===0){
confirm("It is a tie");
console.log("It is a tie");
}
else if (result===-2){
confirm("YOU WIN!!!");
console.log("YOU WIN!!!");
}
else if (result===1){
confirm("YOU WIN!!!");
console.log("YOU WIN!!!");
}
else{
confirm("COMPUTER WINS!");
console.log("COMPUTER WINS!");
}
};
Result(userChoice-computerChoice);


0 votes

permalink

Hi,

Couldn't you wrap the entire function within an if statement?

For example

confirm("Welcome!!!");
if ((userChoice === "rock") || (userChoice === "paper") || (userChoice === "scissors")){
    //enter the rest of your code here
}
else {
alert("Please type your choice correctly");
}

This should make sure the user types in the correct choice, and if they don't the system will pop up and tell them they've made a mistake. At which point they can re-run the program.

I will point out that my if statement is very crude. I don't know how to make the if statement read "if the user choice is rock, paper, or scissors" without entering userChoice multiple times as I've shown.

Hope that helps.

607 points
561cbad5d3292fc620000085_543307503
Submitted by
Avdi
about 3 years ago