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");
}
}
};
Related
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 have been working on this Rochambeau game in JavaScript and so far when I run it, it will prompt for user input but no matter what you input, the result is ALWAYS a tie. Here is my code:
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);
function compare(userChoice, computerChoice){
if(userChoice === computerChoice){
alert("The result is a tie!");
}
else if(computerChoice === "scissors"){
alert("rock wins!");
if(computerChoice === "paper"){
alert("paper wins!");
}
}
else if(computerChoice === "rock"){
alert("rock wins!");
if(computerChoice === "scissors"){
alert("scissor wins!");
}
}
else if(computerChoice === "rock"){
alert("rock wins!");
if(computerChoice === "paper"){
alert("scissors wins!");
}
}
}
compare();
Any idea why this isn't running the rest of the conditions?
You're not passing any parameters to the function. The parameters in the function will default to undefined. So undefined === undefined equals true. You should do this instead:
compare(userChoice, computerChoice);
compare();
Aren't you missing the parameters?
Everything works fine but when i try to re run the function it is stuck on the alert box what did i do wrong and can anyone explain why its happening.You can look at the comment in the code to see the area im getting this probem
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{
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 if (choice1 != "rock"&&"paper"&&"scissors"){
alert("not a viable input,please try again");
compare(userChoice,computerChoice);
//calling the function here makes the alert box repeatedly pop up
}
};
compare(userChoice,computerChoice);
You can set a flag and put everything in a while loop.
var finished = false;
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
finished = true;
return "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
finished = true;
return ("rock wins");
} else {
finished = true;
return ("paper wins");
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
finished = true;
return ("paper wins");
} else {
finished = true;
return ("scissors wins");
}
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
finished = true;
return ("rock wins");
} else {
finished = true;
return ("scissors wins");
}
} else if (choice1 != "rock" && "paper" && "scissors") {
alert("not a viable input,please try again");
finished = false;
//compare(userChoice, computerChoice); This causes recursion. Not necessary.
}
};
while (!finished) {
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);
alert(compare(userChoice, computerChoice));
}
Take a look at this fiddle http://jsfiddle.net/7p94axhp/
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();
}
}