i'm trying to make a rock-paper.. game but it seems that the code works only when i have a tie. I've probably messed it up on the way down.Also i want to ask if the number on the prompt window that you fill in is a string or a number ?
Any bit of help appreciated.Thanks !
// rock beats scissors (1 beats 3)
// paper beats rock (2 beats 1)
// scissors beat paper (3 beat 2)
var player1= prompt("Player ONE, choose (1) for rock, (2) for paper, (3) for scissors");
var player2 = prompt("Player TWO, choose (1) for rock, (2) for paper, (3) for scissors");
function game (player1,player2)
{
if (player1===player2){
alert("its a tie");
}
else
{
if (player1+player2==="4")
{
if(player1==="1"){
alert("Rock beats Scissors, Player one wins");
}else {
alert("Rock beats Scissors, Player Two wins");
}
}
if (player1+player2==="3")
{
if (player1==="1"){
alert("paper beats rock, player One wins");
}else {
alert ("paper beats rock, player Two wins");
}
}
if (player1+player2==="5")
{
if (player1==="3"){
alert("scissors beats paper, Player One wins");
}else{
alert("scissors beats papaer, player Two wins");
}
}
}
};
game(player1,player2);
You're concatenating strings, not adding numbers, so your player1+player2==="3" would actually produce 12 or 21. You want to convert the string to a number first.
Place this code at the top of the else block of your tie check.
player1 = parseInt(player1);
player2 = parseInt(player2);
As an extension, you will want to sanitise the player input to make sure that it only contains digits, as this method will fail if anything other than a numeric string is passed.
You're doing string concatenation and not integral maths on your player1..2 vars.
Notice that you will need to change your comparisons as well, not just parsing the strings to ints.
Try:
var player1= parseInt(prompt("Player ONE, choose (1) for rock, (2) for paper, (3) for scissors"));
var player2 = parseInt(prompt("Player TWO, choose (1) for rock, (2) for paper, (3) for scissors"));
function game (player1,player2)
{
if (player1===player2){
alert("its a tie you mofos");
}
else
{
if (player1+player2===4)
{
if(player1===1){
alert("Rock beats Scissors, Player one wins");
}else {
alert("Rock beats Scissors, Player Two wins");
}
}
if (player1+player2===3)
{
if (player1===1){
alert("paper beats rock, player One wins");
}else {
alert("paper beats rock, player Two wins");
}
}
if (player1+player2===5)
{
if (player1===3){
alert("scissors beats paper, Player One wins");
}else{
alert("scissors beats papaer, player Two wins");
}
}
}
};
game(player1,player2);
In addition to what Ruirize wrote, there is also another mistake:
In the lines player1+player2==="4" you are trying to add two integers and then compare them to a string.
The === operator does not just compare the value, but also the type. When you compare an integer with a string, it will return false.
Either use the == comperator, where the number 4 is equal to the string "4", or compare to the number 4:
player1+player2 === 4
Try this! Hope it helps!
--javascript code for rock, paper, scissors...--
var userChoice = prompt("Please type in your choice : 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 (userChoice, computerChioce)
{
if (userChoice === computerChioce)
{
return "The result is a tie!";
}
else if (userChoice === "rock")
{
if(computerChioce === "scissors")
{
return "rock wins" + " ," + "rock breaks scissors";
}
else if (computerChioce === "paper")
{
return "paper wins" + " ," + "paper captures rock";
}
}
else if (userChoice === "paper")
{
if (computerChioce === "rock")
{
return "paper wins" + ", " + "paper captures rock";
}
else if (computerChioce === "scissors")
{
return "scissors win" + ", " + "scissors cuts paper";
}
}
else if (userChoice === "scissors")
{
if (computerChioce === "rock")
{
return "rock wins" + " " + "rock breaks scissors";
}
else if (computerChioce === "paper")
{
return "scissors win" + ", " + "scissors cuts paper";
}
}
};
console.log("You chose" + " " + userChoice + ".");
console.log("Computer chose" + " " + computerChoice + ".");
compare (userChoice, computerChoice);
Related
I have been trying to make this simple game with javascript on rock paper scissors. The alert command is not showing when I open it up on both Microsoft edge and Chrome. Here is my code, I need helping out on this.
http://pastebin.com/qsPTPWn6
When you do var compare = function().... is defining a function.
so you have a call this function to get a result.
try do
compare("scissors", computerChoice); //at the end of script
This is just an example, assuming you will play scissor every time
First you need to call the function. so it is better to give a function name for calling it, try this :
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 = toCompare(userChoice, computerChoice);
function toCompare(userChoice, computerChoice){
if (userChoice === computerChoice){
alert("The result is a tie!");
}
if(userChoice === "rock"){
if (computerChoice === "scissors"){
alert("rock wins");
}
else{
alert("paper wins");
}
}
if(userChoice === "paper"){
if (computerChoice === "rock"){
alert("paper wins");
}
else{
alert("scissors wins");
}
}
if(userChoice === "scissors"){
if (computerChoice2 === "paper"){
alert("scissors wins");
}
else{
alert("rock wins");
}
}
};
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 ;)
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")
Ok so this should be fairly easy to answer but for some reason im having an issue.( could be because im very new to programming) I've created a rock,papers,scissors game against the CPU. Now i want to ask the user if they want to play again and if they answer Y then it should go through the loop each time. If they type "N" then the game will end. The main issue im having is that once you type in Y to play again it just gives you the result from the last game. Any pointers would help.
Here is what i have :
var userChoice = "";
var userChoice = prompt("Choose rock, paper, or scissors");
var playagain = "Y";
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
choice1 = userChoice;
choice2 = computerChoice;
while (playagain == "Y") {
function compare(choice1, choice2) {
if (choice1 == choice2) {
return ("It's a tie!");
}
if (choice1 == "rock") {
if (choice2 == "scissors") {
return ("You win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "paper") {
if (choice2 == "rock") {
return ("you win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "scissors") {
if (choice2 == "rock") {
return ("computer wins!");
} else {
return ("you win!");
}
}
}
document.write(compare(choice1, choice2));
document.write("<br>");
playagain = prompt("Do you want to play again, Y or N");
userChoice = prompt("Choose rock, paper, or scissors");
}
The only thing you need is to move all game logics under the while loop:
function compare(choice1, choice2) {
if (choice1 == choice2) {
return ("It's a tie!");
}
if (choice1 == "rock") {
if (choice2 == "scissors") {
return ("You win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "paper") {
if (choice2 == "rock") {
return ("you win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "scissors") {
if (choice2 == "rock") {
return ("computer wins!");
} else {
return ("you win!");
}
}
}
}
var playagain = "Y";
while (playagain == "Y") {
var userChoice = prompt("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";
}
document.write(compare(userChoice, computerChoice));
document.write("<br>");
playagain = prompt("Do you want to play again, Y or N");
}
I have:
moved everything except the declaration of playagain into the loop
removed choice1 and choice2 local variables, as they are redundant
removed prompt in the end of the loop, since we have one in the beginning
removed the declaration of an empty userChoice in the beginning
by the advice of #LiYinKing, moved the function out of the loop
Here is the working JSFiddle demo.
Inside of the while loop you need to include your user prompt and new computer generated random number. The problem is your loop is calculating the same random number and user input over and over.
It's hard to explain exactly what's wrong because the code is difficult to follow.
I don't know how you expect the code that sets the computer's choice to run again after the code executes to the bottom of the script.
Declaring named functions in a loop doesn't make sense. Declare them at the top. Then there's nothing left in the body of the loop. Fishy...
What you need is a function that runs the game and a while loop calling that function.
I am on a phone, this is untested
function runGame() {
var userChoice = prompt("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";
}
if(userChoice==computerChoice){
return "It's a tie!";
}
if(userChoice=="rock"){
if(computerChoice=="scissors"){
return "You win!";
} else{
return "computer wins!";
}
}
if(userChoice=="paper"){
if(computerChoice=="rock"){
return "you win!";
} else{
return "computer wins!";
}
}
if(userChoice=="scissors"){
if(computerChoice=="rock"){
return "computer wins!" ;
} else{
return "you win!" ;
}
}
}
alert(runGame());
while ( prompt("Do you want to play again, Y or N") == "Y") {
alert(runGame());
}
Later with experience and knowledge you can write the script like this :
function compare(choice1, choice2) {
if (choice1 == choice2) {
return ("It's a tie!");
}
var lose = {
"rock": "scissors",
"paper": "rock",
"scissors": "paper"
};
return (lose[choice1] == choice2) ? "You win!" : "computer wins!";
}
(function() {
var CHOICE = ["rock", "paper", "scissors"];
var userChoice = "";
var userChoice, computerChoice, playagain, i = 0;
do {
document.write("Ground " + i + " :<br>");
computerChoice = CHOICE[Math.floor(Math.random() * 3)];
do {
userChoice = (""+prompt("Choose rock, paper, or scissors")).toLowerCase();
} while (CHOICE.indexOf(userChoice) === -1);
document.write(userChoice + " vs " + computerChoice + "<br>");
document.write(compare(userChoice, computerChoice) + "<br>");
playagain = (""+prompt("Do you want to play again, Y or N")).toUpperCase();
i++;
} while (["Y", "YES"].indexOf(playagain) !== -1);
});
Explanation :
I add toLowerCase() function to replace "Rock" to "rock", and I add toUpperCase() to replace "y" to "Y".
In this circumstance do { /* script */ } while (bool) is more suitable.
With :
do {
userChoice = (""+prompt("Choose rock, paper, or scissors")).toLowerCase();
} while (CHOICE.indexOf(userChoice) === -1);
You wait a valid answer, three word only : rock or paper or scissors. When userChoice equals "rock" indexOf return 0 ; equals "paper" indexOf return 1...
This syntax :
return (lose[choice1] == choice2) ? "You win!" : "computer wins!";
Is :
if (lose[choice1] == choice2)
return "You win!";
else
return "computer wins!"
JavaScript ternary operator example with functions
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();
}
}