Reputation: 15
I have this code for a simple Rock Paper Scissors game, but when I run it it just asks me for my input of Rock, Paper, or, Scissors, and then nothing else after that. No alert after that all.
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)
{
return alert("The result is a tie!");
}
if (choice1 === "Rock")
{
if (choice2 === "Scissors")
{
return alert("Rock wins!");
}
else if (choice2 === "Paper")
{
return alert("Paper wins!");
}
}
else if (choice1 === "Scissors")
{
if (choice2 === "Rock")
{
return alert("Rock wins!");
}
else if (choice2 === "Paper")
{
return alert("Schissors wins!");
}
}
};
compare(userChoice, computerChoice);
By the way, I am using an online editor.
Upvotes: 0
Views: 2473
Reputation: 3055
The obvious bug is that computer choices are all lowercase while compare
function uses titlecase for Rock, Paper and Scissors.
I will also suggest some improvements to your code. First, compare
function is doing two things now: calculating game result and signaling it to user. A better approach would be to do that separately. Function should do single thing:
function compare(choice1, choice2) {
...
return "It's a tie";
...
return "Rock wins";
...
}
alert(compare(...))
Second, lowercase user input. This will make it same format as computer choice and probably save you from user questions.
Third, check user input if it's one of available variants:
var userChoice = prompt("Do you ...?").toLowerCase();
var variants = ['rock', 'paper', 'scissors'];
if (variants.indexOf(userChoice) === -1) {
alert("Don't cheat! Choose one of ...")
}
Forth, there are only six possible choice pairs in this game, so compare
function should not be that long. One way to make it simpler is by explicitly listing all possible choice pairs:
var choicePairs = {
'rock.rock': "It's a tie",
'rock.paper': "Paper wins",
'rock.scissors': "Rock wins",
'paper.rock': "Paper wins",
...
}
function compare(choice1, choice2) {
return choicePairs[choice1 + '.' + choice2];
}
And last you can reuse variants
array to select computer choice:
var computerChoice = variants[Math.floor(Math.random() * variants.length))];
You can stash this select-random-element-from-array thing in separate function for clarity.
Have a good coding!
Upvotes: 0
Reputation: 208465
Your problem here is that you are mixing cases. You set computerChoice
to either "rock"
, "paper"
, or "scissors"
but then inside of the function you are performing comparisons with "Rock"
, "Paper"
, or "Scissors"
.
Just use lowercase throughout, and add userChoice = userChoice.toLowerCase()
after the prompt()
call.
Upvotes: 1
Reputation: 19027
String comparison in javascript is case sensitive:
"rock" != "Rock"
Upvotes: 0
Reputation: 45252
This will only display anything if choice1
is an exact match with either "Rock"
or "Scissors"
.
You left out "Paper"
. More significantly, you left out the possibility of spelling mistakes by the user.
You should have an 'everything else' branch. This will at least give you some feedback.
var compare = function (choice1, choice2)
{
if (choice1 === choice2)
{
return alert("The result is a tie!");
}
if (choice1 === "Rock")
{
if (choice2 === "Scissors")
{
return alert("Rock wins!");
}
else if (choice2 === "Paper")
{
return alert("Paper wins!");
}
}
else if (choice1 === "Scissors")
{
if (choice2 === "Rock")
{
return alert("Rock wins!");
}
else if (choice2 === "Paper")
{
return alert("Schissors wins!");
}
}
alert('Unhandled combination! choice1 = ' + choice1 + ', choice2 = ' + choice2);
};
Upvotes: 0
Reputation: 8054
No return
s needed.
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) {
alert("The result is a tie!");
}
if (choice1 === "Rock") {
if (choice2 === "Scissors") {
alert("Rock wins!");
} else if (choice2 === "Paper") {
alert("Paper wins!");
}
} else if (choice1 === "Scissors") {
if (choice2 === "Rock") {
alert("Rock wins!");
} else if (choice2 === "Paper") {
alert("Schissors wins!");
}
}
};
compare(userChoice, computerChoice);
Upvotes: 1