9/9 Adding appropriate prompt?


#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 chooses: " + computerChoice);

So I have the first part of the code here, and I want to make sure that whatever the user inputs is one of the proper three choices and am not sure how to go about doing that. Is this a reasonable solution or totally wrong? If I add the following...

  else if userChoice !== ("rock", "paper", "scissors") {
        return "Please make a valid selection.";
    }


#2

Hi @mattwilki,

If it may help please see the code the sample below.
There are many ways to handle this really, The below example has tried to use nothing but code and concepts that hopefully one should have encountered if doing the courses here on Codecademy, so at the risk of maybe dumbing it down if your ability is high, this should still give some general idea on one of the many ways one could go about accomplishing this :), and maybe help someone else with a similar question

If anything is not clear please feel free to post back with any questions, someone will try to help when possible, and when I can I will certainly try to answer anything behind why I used the below method for example, no doubt there is plenty of others out there with ideas on how to handle this too. :slight_smile:

```
var userChoice = ""; //declare the var here to give it global scope per se

function UsersTurn() { //this is our starting function where we get the user input
    userChoice = prompt("Do you choose rock, paper or scissors?");
    ValidChoice();
}

function ValidChoice() { //this function will 'validate' the user input to our restricted selection
    if (userChoice.toLowerCase() != 'rock' &&  
        userChoice.toLowerCase() != 'paper' &&
        userChoice.toLowerCase() != 'scissors')
    { //if the user didn't have a valid selection DO SOMETHING EG: Let them know and start again maybe
        alert("Sorry only enter rock, paper or scissors");
        userChoice = "";
        UsersTurn();
    }
    else {
        PlayRPS(); //if the user did make a valid selection then DO SOMETHING ELSE EG: Lets play RPS
    }
}

function PlayRPS() { //this function is the actual body of your code that will handle game play for now
    var computerChoice = Math.random(); {
        if (computerChoice < 0.34) {
            computerChoice = "rock";
        } else if(computerChoice <= 0.67) {
            computerChoice = "paper";
        } else {
            computerChoice = "scissors";
        } 
    };
    console.log("Computer chooses: " + computerChoice);

    //add the rest of your code in this code block

}

UsersTurn(); //upon start we want to call the function related to starting....

`


#3

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