Reputation:
Hi I am a newbie in JavaScript.I was trying to build a basic JavaScript rock paper scissor game..
I have implemented the below code.
The error I am getting is that while returning the value and displaying the return statement in the HTML I am always getting "The result is a tie",even though the computer and the user choices are different. I don't know where I have gone wrong.
Please help me out.The below is the code I have tried
<html>
<head>
<title>Input tutorial</title>
<script language="javascript">
var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log(userChoice)
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67 && computerChoice > 0.34) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
alert("Computer: " + computerChoice);
var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice)
{
console.log("Here I amk")
return "The result is a tie!";
}
else if (userChoice === "rock"){
if(computerChoice === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
else if (userChoice === "paper"){
if(computerChoice === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}
else if (userChoice === "scissors"){
if(computerChoice === "paper") {
return "scissors wins";
}
else {
return "rock wins";
}
}
else {
console.log(error)
}
};
//document.write(compare());
</script>
</head>
<body>
<h1>
Rock Paper Scissor Game
</h1>
<h1>
The result is <script>document.write(compare());</script>
</h1>
</body>
</html>
Upvotes: 0
Views: 61
Reputation: 1290
You do not want userChoice and computerChoice
to be parameters to your compare
function.
You are setting these up by creating local variables prior to running the compare
method, but overriding them in the function's local scope by naming them as parameters as well. Removing those parameters would be the first step to a right solution.
This is because you now have an outer scope and inner scope within the compare
function, both with userChoice
and computerChoice
that mean completely different things. If you had any other choice of naming these parameters, after it doesn't find userChoice
in the local function scope, it expands and looks at the parent element scope and find the code you previously wrote.
Upvotes: 0
Reputation: 2104
Short answer: You need to pass the values into the compare function:
The result is <script>document.write(compare(userChoice, computerChoice));</script>
The suggestion by a couple of other posters to remove the parameters in the function definition will work, but it is better to solve it this way round and post the values to the function. Try to ensure the function does not rely on global variables to work.
Tip for the future:
You need to look at the difference between local and global variables. When you define values in a function, the global values that you defined earlier are no longer valid.
Upvotes: 0
Reputation: 1
you're calling compare like this
compare()
so the values of the arguments will be undefined
undefined === undefined
so, it's a tie
simplest fix is to remove the arguments from compare
compare = function ()
Upvotes: 3