Rock, paper, scissors..If user input undefined


#1

I am trying to extend the code so that if a users input is undefined in the program it will prompt them to only use rock, paper, or scissors.. any tips? It seems like something you would use a lot in the real world..

Here's what I've got and it's not working, when I enter something like "dog", I just get this error

ReferenceError: choice is not defined

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);
console.log(compare(userChoice,computerChoice))

var compare = function(choice1, choice2) {
if(userChoice !== "rock","paper","scissors") {
return "You must choose from Rock, Paper, or Scissors to play!";
}
else if(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 win";
}
}
else if(choice1 === "rock") {
if(choice2 === "scissors") {
return "rock wins";
}
else {
return "paper win";
}
}
else if(choice1 === "scissors") {
if(choice2 === "paper") {
return "scissors win";
}
else {
return "rock";
}
}
};


#2

before the last bracket add an else statement similar to mine:

    else {
        console.log("Please choose either Rock, Paper, or Scissors!");
    }
}; //End of the function

#3

Hmm, I'm getting an error : TypeError: compare is not a function.


#4

I'm getting the error when I remove the last block too.. Maybe I messed something else up while removing the first block.


#5

Just revert back to what you had before hand. I'm pretty sure you do not need that last else statement to make you code work so just re-write what you had before you added in my code.


#6

@joshmoe830, 2 things :

About => console.log(compare(userChoice,computerChoice))
* you cannot call your function there cause for the computer you didn't create it yet. You need to do - compare(userChoice,computerChoice); - after creating - var compare = function(choice1, choice2) { //code }; -

About => var compare = function(choice1, choice2) {
if(userChoice !== "rock","paper","scissors") {
return "You must choose from Rock, Paper, or Scissors to play!";
* when you set the condition you need to specify AND which is && to basically say if userChoice is neither equal to this that and that
So : if ((userChoice !== "rock") && (userChoice !== "paper") && (userChoice !== "scissors")) {
// code }


#7

@virginie_t,
You do know this
= http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1
which explains the =named=function being hoisted...


#8

No I didn't ! So far I was always writing my function like function functionName() { ... }
Thanks for sharing :smiley: