9 - Create your own game


#1

I have been messing with the code from the game to make sure it doesn't accept any inputs except rock, paper and scissors. If it find a different input it should re-prompt the question. Else move on through the rest of the program.

Currently any input generates a false statement.

I am sure I took some shortcuts that don't work. Any help / suggestions?

Currently the program returns only a false statement but doesn't re-prompt.

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

if (userChoice = !"rock" && !"paper" && !"scissors") {
var userChoice = prompt("Please chose only rock, paper or scissors!");}
else
{
console.log ("You: " + userChoice);
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 (userChoice, computerChoice) {
if (userChoice === computerChoice) {
return "The result is a tie!";}
else if (computerChoice === "rock") {
if (userChoice === "scissors") {
return "Rock Wins! You lose!"}
else {
return "Paper Wins! You WIN!"}
}
else if (computerChoice === "paper") {
if (userChoice === "rock") {
return "Paper Wins! You lose!"}
else {
return "Scissors Win! You WIN!"}
}
else if (computerChoice === "scissors") {
if (userChoice === "paper") {
return "Scissors Win! You lose!"}
else {
return "Rock Wins! You WIN!"}
};
};

var result = compare (userChoice, computerChoice);
console.log (result);
};



#2

@gigarockstar30425 take a look at this post to get an idea what to do
Click here to post


#3

I'm not an expert but I believe your if conditions are not in the right syntax.

Your if statement...
if (userChoice = !"rock" && !"paper" && !"scissors") {
Try...
if (userChoice != "rock" && userChoice != "paper" && userChoice !="scissors") {

Also, if the user doesn't enter something correct the first time it will not run the stuff in the else so the computer won't have a choice selected, so it won't work this way.

Since we aren't using loops yet, I put the selection in a function so that I can rerun it by calling it. Try this:

var userChoice;
var computerChoice;

var getChoices = function() {
userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice != "rock" && userChoice != "paper" && userChoice !="scissors") {
getChoices();
}
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! Lets do this again.");
getChoices();
return compare(userChoice,computerChoice);
} 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 === "rock") {
return "rock wins";
}else {
return "scissors wins";
}
} else {
return "invalid entry by player";
};
};

compare(userChoice,computerChoice);


#4

Thank you @etx2 your post was very helpfull. This is the only solution I've been able to find on forum that includes past lessons not the future ones, which I believe was intended by the maker, and only one I found to make sense.
The search for a solution can be quite a headache so I've decided to contribute to this working code in one small part, for anyone searching for a solution to this exercise.
I am not an expert also, this is my conclusion from the codes result, if anyone would like to correct my observation, I would be much obliged.
.
The problem in the code written would be line:

getChoices();

in function getChoices.

Correction would be to have return statement when calling a function again so it would stop there, like so:

return getChoices();.

Explanation:
In case that userChoice is invalid answer, lets say a bird, player is asked again and again until he answers rock, paper or scissors- so that's working.
But the problem is that the function will continue to run and printout computer choice for every time player inputs some answer: 3 answers ( 2 wrong one and 1 correct )-3 computerChoice prints out ( 3 random choices). I hope I am clear enough.

I haven't realy checked the rest of the code, I presume is corect, exept the last line which I suppose was left by accident from previous code?:
else {
return "invalid entry by player";
};

Cheers :smile: