Javascript Rock, Paper, Scissors game - javascript

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?

Related

Javascript: Calling a function inside another function

First function contains the input and the second has if/else logic. I'm calling second function inside first but the if/else statements are seems to not working.
var choice = function(){
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);
compare(userChoice, 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{
return "invalid input";
}
};
choice();
It works well.
compare(userChoice, computerChoice)
returns an appropriate result.
However, you did not display it. So display it with alert(), etc.:
alert(compare(userChoice, computerChoice));

Alert command isnt working on my javascript code

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");
}
}
};

What is the reason my function wont restart?

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/

Looping a rock-paper-scissors game

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

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