1

At this moment, I am trying to learn javascript, but it is giving me a hard time.

I have written a small rock paper scissors script, which should give some output to the user, but I seem unable to actually get that output.

The js script is like this:

var computerChoiceLine = 0
var tieResult = "The result is a tie!";
var winResult = "You won!";
var lossResult = "You lost..."
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
// convert computerChoice into text
if (computerChoice <= 0.34) {
    computerChoiceLine = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
// convert userChoice into number
if (userChoice == "rock") {
    userChoiceNumber = 0;
} else if(userChoice == "paper") {
    userChoiceNumber = 0.50;
} else if(userChoice == "scissors"){
    userChoiceNumber = 1;
} else {
    userChoiceNumber = 100;
}

var output = function(choice1, choice2){
document.write("You chose " + choice1)
document.write("\n The computer chose " + choice2)
compare(userChoiceNumber , computerChoice)

var compare = function(choice1 , choice2){
    if(choice1 == choice2){
        document.write(tieResult);
    }
    if(choice1 < choice2){
        document.write(lossResult);
    }
    if(choice1 > choice2){
        document.write(winResult);
    }
};

if(userChoice == 100){
document.write("Please type rock, paper or scissors")
} else {
output(userChoice , computerChoiceLine);
};
}

And I call for it in HTML like this:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="RockPaperScissors.js"></script>
</body>
</html>

Can anyone please explain what I am doing wrong, and how I should change it?

4
  • 2
    What errors do you get? Commented Aug 11, 2013 at 20:40
  • You call compare before actually defining it. Do you miss the output of tieResult? Commented Aug 11, 2013 at 20:42
  • As I see, I have defined compare, before calling it. And I don't get any errors, I just get a blank web page, and it does doe the prompt, but the page remains blank Commented Aug 11, 2013 at 20:43
  • never mind, with the answer of @skishore this issue is resolved Commented Aug 11, 2013 at 20:46

1 Answer 1

1

You've got a missing close brace at the end of your definition of output and an extra close brace at the end of your js. After I made those changes the script worked.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton, that made it work! And testing it, made me realise that my compare function makes no sense XD

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.