Rock paper, scissors game - javascript

I just finished making a rps game on codecademy, and there are some extra tasks that include making an outcome if the user inputs wrong info. Basically, if you don't write an input of "rock", "paper" or "scissors" it will ask you again until you put in the right one.
How can i call userChoice variable until it gets the right answer. I tried it like this, but it just asks 2 times and then posts whatever you write.
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice != "rock", "paper", "scissors") {
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";
}
var compare = function (choice1, choice2) {
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 wins";
}
}
else if(choice1 === "scissors") {
if(choice2 === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}
}
console.log("Human: " + userChoice);
console.log("Computer: " + computerChoice);
compare(userChoice, computerChoice);

Use a function that references itself:
var userChoice = function () {
var choice = prompt("Do you choose rock, paper or scissors?");
if (choice !== "rock" && choice !== "paper" && choice !== "scissors") {
return userChoice();
} else {
return choice;
}
};
This fiddle has the whole thing working.

Use the following
do {
userChoice = prompt("Do you choose rock, paper or scissors?");
} while (userChoice != "rock", "paper", "scissors")

Related

Javascript, case insensitive, rock, paper, scissors

I have made a rock, paper, scissors game using javascript and have completed the basic function: rock beats scissors etc. but now I am trying to make it case insensitive with the answers please help and thanks.
this is my code if it helps
var userChoice = prompt("Do you choose rock, paper or scissors?Type your answer. Refresh page to start again!");//Initial dialogue
var computerChoice = Math.random();//computer chooses a random number
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
//Above: if computer chooses a number it is equal to rock, paper or scissors
var compare = function(choice1, choice2) //variable of computer and user choice
{
if(choice1 === choice2) {
return console.log("The result is a tie!)";//If the user and computer chooses the same then text
}
if(choice1 === "rock" ) {
if(choice2 === "scissors") {
return console.log("rock wins");
} else {
return console.log("paper wins");
}
}
if(choice1 === "paper") {
if(choice2 === "rock") {
return console.log("paper wins");
} else {
if(choice2 === "scissors") {
return console.log("scissors wins");
}
}
if(choice1 === "scissors") {
if(choice2 === "rock") {
return console.log("rock wins");
} else {
if(choice2 === "paper") {
return console.log("scissors wins");
}
}
}
} //Abover: Rock beats scissors, paper beats rock, scissors beats paper and dialogue
console.log("User Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
compare(userChoice, computerChoice);
To make JS case insensitive, transform the text to lowercase :
var text = 'InPuT';
text = text.toLowerCase();
So InPuT becomes input and then you can test it like :
if(text=='input'){ //... }
Hope it helped you ;)

Calling parameters from a function

I'm trying to learn javascript and this lesson is having me create a rock, paper, scissors game. It is asking for me to:
Call your function and pass in userChoice and computerChoice as your two arguments.
I am not sure how to pass in userChoice and computer Choice. I tried doing this:
console.log(compare + userChoice + computerChoice)
But obviously it isn't correct.
What am I doing wrong??
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) {
return "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
} else {
"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";
}
}
}
console.log(compare + userChoice + computerChoice)
You're VERY close, arguments are passed to functions like this:
compare(userchoice, computerChoice);

Javascript Rock, Paper, Scissors [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I followed the tutorial on Javascript on http://www.codecademy.com and one part was to create a rock, paper, scissors game. After I finished it I figured I would redesign it a bit to use on my own. So I did this:
<!DOCTYPE html>
<html>
<body>
<script>
var userChoice = document.getElementById("userChoice").value;
if (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors"){
alert("Your choice was not rock, paper or scissors");
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";
}
var compare = function(choice1, choice2) {
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 wins";
}
} else if (choice1 == "scissors") {
if (choice2 == "paper") {
return "Scissors win";
} else {
return "Rock wins";
}
}
}
function Run() {
document.getElementById("pcc").innerHTML = "Computer: " + computerChoice;
document.getElementById("result").innerHTML = compare(userChoice, computerChoice);
}
</script>
<main>
<h2>Choose between rock, paper or scissors: </h2>
<input id="userChoice">
<button onClick="Run()">Choose</button>
<p id="pcc"></p>
<p id="result"></p>
</main>
</body>
</html>
But it doesn't work, I get undefined for compare, computerChoice, userChoice and that there isn't anything that becomes stored in value.
What's wrong?
You need to put all of the content of the script in a function, try this. It is trying to use the variables before they are set by the logic at the beginning since the logic at the beginning is never called:)
function run(){
var userChoice = document.getElementById("userChoice").value;
if (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors"){
alert("Your choice was not rock, paper or scissors");
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";
}
var compare = function(choice1, choice2) {
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 wins";
}
} else if (choice1 == "scissors") {
if (choice2 == "paper") {
return "Scissors win";
} else {
return "Rock wins";
}
}
}
document.getElementById("pcc").innerHTML = "Computer: " + computerChoice;
document.getElementById("result").innerHTML = compare(userChoice, computerChoice);
}
You should remove the <main> tags and correctly close your second paragraph tag like so:
<p id="result"></p>

How to make it so only valid options can be chosen for rock paper scissors

Ive made a small Rock paper scissor game with javascript. There are two things that I would like to do to make the game a little better.
1) If the player gives an answer other than rock, paper or scissors for a prompt saying "Please pick between one of the three options: rock, paper or scissors"
I have implemented something like this but it only goes one run. I would like to have the prompt come up until one of the three answers is given
2) I would like to have the game start running code from the top again if it is a tie. How could I get the program to start from the top again?
Here is the code
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice === "rock")
{}
else if (userChoice === "paper"){}
else if (userChoice === "scissors"){}
else {
userChoice = prompt ("Please pick between one of the three options: 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( "The computer's choice is" + " " + computerChoice);
console.log("Your choice was " + userChoice);
var compare = function(choice1 , choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
if (choice1 === "rock")
{
if (choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
if (choice1 === "paper")
{
if (choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else
{
return "scissors wins";
}
}
};
compare(userChoice, computerChoice);
What you are doing in this program is only 'Conditional statement' checking. There is another construct to Structured Programming that is called 'Loops'
Generally it is humanly possible to copy-paste a segment of code 1000s of times and maybe even millions but not infinite. So for such cases programmers use Loops that start with an initial state, execute the same body of code as long as a given condition holds and follows a state change.
You can use a while loop in this case as it is the simplest structure for such scenarios. It only takes the 'condition' for which it much keep executing the body of code. The structure of while loop is as such.
while ( condition ) {
<the code that you want it to keep executing>
}
If we break your program down into various parts there are mainly 2 parts to it.
1. Take the input
2. Check if the input is valid or not. If not take the input again.
From this you can easily see what should be the condition for your loop.
"While the input is not valid"
So it's like
while ( the input is not valid / the userChoice is not rock or paper or scissors ) {
take the input
}
To make your loop infinite you can use
while ( true ) {
take the input
}
To break out of this infinite loop, you use "break;" anywhere inside it. So a 'break;' inside an if statement will get out of this loop like this
while ( true ) {
if ( condition ) {
break;
}
}
So, if we follow your style of condition checks we get.
var userChoice = prompt("Do you choose rock, paper or scissors?");
while ( true ) {
if (userChoice === "rock")
{break;}
else if (userChoice === "paper"){break;}
else if (userChoice === "scissors"){break;}
else {
userChoice = prompt ("Please pick between one of the three options: rock, paper or scissors");
}
}
But using 'breaks' is a bad programming practice. So why don't we take the advantage of while's 'condition'?
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") {
userChoice = prompt("Please pick between one of the three options: rock, paper or scissors");
}
And since you want the Game to go on forever, we can put the whole game inside a loop right? But that will totally take your control off your browser and you can't do anything while that loop is waiting for your response. So we keep a way out for ourselves? Why don't we add another condition that, if the user types "EXIT" the game stops?
while ( true ) {
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "EXIT") {
userChoice = prompt("Please pick between one of the three options: rock, paper or scissors");
}
if (userChoice === "EXIT") {
console.log("Thanks for playing :)");
break;
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log( "The computer's choice is" + " " + computerChoice);
console.log("Your choice was " + userChoice);
var compare = function(choice1 , choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
if (choice1 === "rock")
{
if (choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
if (choice1 === "paper")
{
if (choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else
{
return "scissors wins";
}
}
};
compare(userChoice, computerChoice);
}
Now, you can make this much easier to Modify later (for you and others) by breaking your program down into functions just the way you did for the Compare part. Then taking the decision on the userInput will be much more robust process.
function take_user_input() {
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "EXIT") {
userChoice = prompt("Please pick between one of the three options: rock, paper or scissors");
}
return userChoice;
}
function play(userChoice) {
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log( "The computer's choice is" + " " + computerChoice);
console.log("Your choice was " + userChoice);
var compare = function(choice1 , choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
if (choice1 === "rock")
{
if (choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
if (choice1 === "paper")
{
if (choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else
{
return "scissors wins";
}
}
};
compare(userChoice, computerChoice);
}
while ( true ) {
var userChoice = take_user_input();
if (userChoice === "EXIT") {
console.log("Thanks for playing :)");
break;
} else {
play(userChoice);
}
}
And after that, you can study more on reading DOM elements and modifying them using jQuery / modifying the HTML by Javascript. :) But that brings a totally new topic. But I'd suggest you to look that way. Nobody would love to play Rock Paper Scissors using Console Log when they can do that using Graphical User Interface.
You could wrap the game in a function, which you then call at any point. Maybe put the initial prompt in its own function, if its valid, call the game function.
function getUserInput()
{
// put prompt and validation logic here, then call when its a tie
// You might try consolidating the validation logic with regular expression:
// This returns an array of matches if only rock, paper or scissors was entered.
userChoice.match(/(rock|paper|scissors)/)
if(userChoice)
{
runGameLogic();
}
}

what wrong with this paper and rock mini game?

It return no error but I sense something wrong in codeacademy's console. It return single string - "rock", "paper" or "scissors". I couldn't detect what's wrong.
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";
}
function compare(choice1, choice2){
if(choice1 == choice2) return "The result is a tie!";
if(choice1 == "rock"){
if(choice2 =="scissors") return "rock wins"
else return "paper wins";
}
if(choice1 == "paper"){
if(choice2 =="scissors") return "scissors wins"
else return "paper wins";
}
if(choice1 == "scissors"){
if(choice2 =="paper") return "scissors wins"
else return "rock wins";
}
compare(userChoice, computerChoice);
}
Any bug in the logic and the program flow?
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";
}
function compare(choice1, choice2){
if(choice1 == choice2) return "The result is a tie!";
if(choice1 == "rock"){
if(choice2 =="scissors") return "rock wins"
else return "paper wins";
}
if(choice1 == "paper"){
if(choice2 =="scissors") return "scissors wins"
else return "paper wins";
}
if(choice1 == "scissors"){
if(choice2 =="paper") return "scissors wins"
else return "rock wins";
}
}
compare(userChoice, computerChoice);
You were calling compare() inside the definition of compare.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
// Sanity check. This is the result if there is no a tie, win or loss.
var userResult = 'You must choose either rock, paper or scissors.';
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
function compare(choice1, choice2) {
if (choice1 === choice2) {
userResult = "It is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
userResult = "You win! Rock smashes scissors";
} else {
userResult = "You lose! Paper covers rock";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
userResult = "You win! Paper covers rock";
} else {
userResult = "You lose! Scissors cut paper";
}
} else if (choice1 === "scissors") {
if (choice2 === "paper") {
userResult = "You win! Scissors cut paper";
} else {
userResult = "You lose! Rock smashes paper";
}
}
return userResult;
}
compare(userChoice, computerChoice);
I added a little sanity check for when someone tries to throw 'potato' or something that is not rock, paper or scissors. Also, made it clear who won or lost and why. Sorry, I couldn't resist.

Categories