简体   繁体   中英

Javascript innerhtml Display

Hi I made a js script for coin tossing and when I press the button to start it nothing appears

    <script>
    var coin = Math.random();
    if(coin <= 0.5){
     return "H";
    }
    else{
    return "T";
    }

    var coinFlip = function(){
      document.getElementById("flip").innerHTML = coin;
    }
  </script>

This is the button

<button onClick="coinFlip()">Flip</button>

Thank you for reading

coin is a random number the return is not doing what you think.

 var coinFlip = function() { var coin, chance = Math.random(); if (chance <= 0.5) { coin = "H"; } else { coin = "T"; } document.getElementById("flip").innerHTML = coin; } document.getElementById("btn").addEventListener("click", coinFlip); 
 <div id="flip"></div> <button id="btn">Flip</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM