0

Is there any reason why this code isn't working? I have put the entire script into a function for the sake of the playAgain variable (see bottom) which would restart the function. Any help would be much appreciated.

function headsTails() {
    var userChoice;
    userChoice = prompt('Heads or Tails');

    function myGame(heads,tails) {  
        var result;
        var coin;
        result = Math.random()
        if(result > 0.5) {
            coin = "heads";
        } else {
            coin = "tails";
        }
        if(userChoice === "heads") {
            if(coin = "heads") {
                alert("You win!");
            } else if(coin = "tails") {
                alert("You lose!");
            }
        }
        if(userChoice === "tails") {
            if(coin = "heads") {
                alert("You lose!");
            } else if(coin = "tails") {
                alert("You win!");
            }
        }
    }
    myGame();
    var playAgain;
    playAgain = confirm(Do you want to play again?)
    if(playAgain) {
        headsTails();
    } else {
        alert("Thanks for playing!")
    }
}
7
  • In what way is this not working? Also, "Sorry about the messy spacing" -- really? Just fix it! It would make it less likely that someone capable of answering would simply move on without bothering. Commented Mar 12, 2014 at 22:01
  • 3
    Shouldn't confirm(Do you want to play again?) be confirm('Do you want to play again?')? Commented Mar 12, 2014 at 22:02
  • Formatting is important because it can help you realize why your code might not be working. Check it again after the reformatting and see if you can tell why it might not be doing what you expect. Commented Mar 12, 2014 at 22:05
  • 1
    In regards to spacing/formatting, jsbeautifier.org is great for formatting code before posting Commented Mar 12, 2014 at 22:08
  • I'm confused that you are using === in the conditions, immediately followed by = in the next condition. Commented Mar 12, 2014 at 22:21

1 Answer 1

1

You have many syntax errors:

coin = "heads" -> coin === "heads"
coin = "tails" -> coin === "tails"
confirm(Do you want to play again ?) -> confirm("Do you want to play again ?")

I would recommend reading about Javascript and in general about programming languages...

Corrected headsTails function:

function headsTails() {
    var userChoice;
    userChoice = prompt('Heads or Tails');

    function myGame(heads, tails) {
        var result;
        var coin;
        result = Math.random();
        if (result > 0.5) {
            coin = "heads";
        } else {
            coin = "tails";
        }
        if (userChoice === "heads") {
            if (coin === "heads") {
                alert("You win!");
            } else if (coin === "tails") {
                alert("You lose!");
            }
        }
        if (userChoice === "tails") {
            if (coin === "heads") {
                alert("You lose!");
            } else if (coin === "tails") {
                alert("You win!");
            }
        }
    }

    myGame();
    var playAgain;
    playAgain = confirm("Do you want to play again ?")
    if (playAgain) {
        headsTails();
    } else {
        alert("Thanks for playing!")
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this has solved all of my problems concerning the subject.

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.