needhelpohgodohgod
needhelpohgodohgod

Reputation: 31

Javascript Coin Flip Beginner

<script>

function myfunction () {
    var x = Math.random();

    if (x > 0.5) {
        document.write("Heads!");
    } else {
        document.write("Tails!");
    }
}

</script>

The following code is supposed to print either Heads! or Tails! as if a coin was being flipped. However, it does not seem to work.

Upvotes: 2

Views: 87

Answers (1)

Majed Badawi
Majed Badawi

Reputation: 28404

You are not calling the function:

function myfunction () {
     var x = (Math.random());
     if (x > 0.5) {
          document.write("Heads!");
     }else {
          document.write("Tails!");
     } 
}
myfunction(); //add this

Upvotes: 1

Related Questions