0

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>
4
  • try removing the language="javascript" usually its just type="text/javascript" Commented Oct 11, 2015 at 7:57
  • also both userchoice and computerchoice are undefined Commented Oct 11, 2015 at 8:02
  • 1
    there is a lot more going wrong with this code. document.write is useful if you want to support netscape navigator 1.0 or IE1 or mosaic - just don't do it - I don't want to discourage you from a path in javascript coding, but you need to look at more modern tutorials if you are serious about it Commented Oct 11, 2015 at 8:07
  • 1
    Thanks a lot for the help..need to learn a lot and become like you guys in the future Commented Oct 11, 2015 at 8:10

3 Answers 3

3

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 ()
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.