lundi 9 novembre 2015

Displaying one of two image (50 50 chance-coin toss) from a click button-javascript

I am trying to display one of two images using a click button using random number generator by assigning each image to an if and else statement. I'm not sure where the problem is. Here is what I have:

 <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Assignment 9</title> 
        <link href="images/avatar.png" rel="shortcut icon" type="image/png">
        <link href="css/stylesheet.css" rel="stylesheet" type="text/css">
        <script src="js/javascript.js" type="text/javascript"></script>
        <style type="text/css">
        .auto-style1 {
            text-align: center;
        }
        </style>
        <script type="text/javascript">
        </script>
    </head>
    <body>
    <header>
    </header>
    <aside>
    </aside>
    <div id="main">
    <h1> Arrays and Coditional Statements</h1>

<h2 class="auto-style1">Flip a coin </h2>


<script>

  function myFunction1() {
    var result = doucment.getElementById("pic");
    var x = Math.floor(Math.random() * 2);
    console.log(x);
    if (x == 0) {
      document.getElementById("Coin").innerHTML = "Heads"

 result.innerHTML="<img alt=\"the frontside of a coin\" src=\"images/heads.jpg\" />";  }
  else {
      document.getElementById("Coin").innerHTML = "Tails";
      result.innerHTML= "<img alt=\"the backside of a coin\" src=\"images/tails.jpg\" />";
    }
  }

  myFunction1();
</script>
<button type="button" onclick="myFunction1()" id="Coin">CLick ME</button>
<p id="pic"> </p>






</div>
<footer>
</footer>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire