I\'m working on making my first game (Rock Paper Sissors) and I ran into an issue where when the userChoice is scissors and the com
var compare = function (choice1, choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
else
{
if(choice1 === "rock")
{
if(choice2 === "paper")
{
return "Paper beats rock. Computer Wins.";
}
else
{
return "Rock beats scissors. You win.";
}
}
else
{
if(choice1 === "paper")
{
if(choice2 === "rock")
{
return "Paper beats rock. You Win.";
}
else
{
return "Scissors beat paper. Computer Wins."; }
}
if(choice1 === "scissors")
{
if(choice2 === "rock")
{
return "Rock beats scissors. Computer Wins.";
}
else
{
return "Scissors beat paper. You Win."; }
}
}
}
};
var r = function(user)
{
while(user < 0 | user >3)
{user = prompt("Please don't act oversmart. Press '1' for rock, '2' for paper, and '3' for scissors.");
}
if(user === "1")
user = "rock";
else
{
if(user === "2")
{user = "paper";}
else
{user = "scissors";}
};
console.log("You chose: " + user);
computerChoice = Math.random()
if(computerChoice <= 0.33)
{
computerChoice = "rock";
}
else
{
if(computerChoice > 0.33 && computerChoice <=0.66)
{computerChoice = "paper";}
else
{computerChoice = "scissors";}
}
console.log("The computer chose: "+computerChoice)
console.log(compare(user, computerChoice));
if(user===computerChoice)
{
userChoice = user;
return "1";}
};
var userChoice = prompt("Press '1' for rock, '2' for paper, and '3' for scissors")
var computerChoice;
var a = r(userChoice);
if(a === "1")
{//console.log("1");
while(userChoice === computerChoice)
{
var a = prompt("Since there was a tie, please choose again. Press 1 for rock, 2 for paper and 3 for scissors.")
var b = r(a);
if(b !== "1")
{break;}
}
}
You were unable to see the issue most likely due to poor indentation of your code. Properly indented the issue is clear:
if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
if (choice2 === "scissors") {
return "scissors wins";
}
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
if (choice2 === "paper") {
return "scissors wins";
}
}
}
}
Your if (choice1 === "scissors") {
is within if (choice1 === "paper") {
. The code within will never be reached.
I got this to work:
function playFunction() {
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";
}
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
alert("The result is a tie!");
}
if(choice1 === "rock") {
if(choice2 === "scissors") {
alert("rock wins");
} else {
alert("paper wins");
}
}
if(choice1 === "paper") {
if(choice2 === "rock") {
alert("paper wins");
} else {
if(choice2 === "scissors") {
alert("scissors wins");
}
}
if(choice1 === "scissors") {
if(choice2 === "rock") {
alert("rock wins");
} else {
if(choice2 === "paper") {
alert("scissors wins");
}
}
}
}
};
console.log("User Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
compare(userChoice, computerChoice)
}
All I changed was instead of returning a message, it pops up an alert with the answer. I also put it into one function that could be called on an HTML button click.
This is the code I made at this exercise and it worked like a charm... I used logical operators on my "if" statements, and it was accepted(obviously).
Give it a try :D
var userChoice = prompt("Do you choose rock, paper or scissor?");
var computerChoice = Math.random();
if (computerChoice > 0 && computerChoice < 0.33) {
computerChoice = "Rock";
} else if (computerChoice > 0.34 && computerChoice < 0.67) {
computerChoice = "Paper";
} else {
computerChoice = "Scissor";
}
console.log(computerChoice);
Try This :
var UserChoice = window.prompt("Do you choose rock, paper or scissors ?");
var computChoice = Math.random();
var computChoice = computChoice < 0.34 ? "rock" : ( computChoice > 0.67 ? "scissors" : "paper" ) ;
var mess = {
rock : { scissors : 'You Win!, Rock smashes scissors!', paper : 'You lose!, Paper covers rock!'} ,
paper : { rock : 'You Win!, Paper covers rock!', scissors : 'You lose!, Scissors cut paper!' },
scissors : { paper : 'You Win!, Scissors cut paper!', rock : 'You lose!, Rock smashes scissors!' }
}
if ( computChoice === UserChoice)
result = "It's a tie!" ;
else if ( UserChoice !== "rock" && UserChoice !== "paper" && UserChoice !== "scissors" )
result = "Invalid choice! Choose from rock, paper, or scissors" ;
else
result = mess[UserChoice][computChoice] ;
console.log( 'you chose ' + UserChoice + ' and computer chose ' + computChoice + ' ( ' + result + ' ) ') ;
The solution I came to as a fellow novice seems relatively simple..
var userChoice = prompt ("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice <=0.33) {
"rock";
} else if (computerChoice <=0.66) {
"paper";
} else {
"scissors";
}