Javascript random number and check its value - Javascript Data Type

Javascript examples for Data Type:Number

Description

Javascript random number and check its value

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body>   
      <script type="text/javascript">
function WinLose()/*  ww  w .j  a va  2s.  c om*/
{
    var x=document.getElementById("demo");
    x=x.innerHTML=Math.floor((Math.random()*5)+1);
    if (x==4) {
        console.log("winner!");
    } else {
        console.log("loser");
    }
    return x;
}

      </script>   
      <p id="demo">Click the button to display a random number between 1 and5.</p> 
      <button onclick="WinLose()">Try it</button>    
   </body>
</html>

Related Tutorials