JSFiddle - React, Tailwind, and code Playground

by Yuan Hong

HTML

<p>input win rate between 0 and 1 </p>

<input type="text" id="rate"></br>
<button onclick="myFunction()">Try it</button>

<p id="demo" class= test></p>

CSS

.test {
    width: 150px;
    height: 20px;
    background: #ffb;
    padding: 10px;
    border: 2px solid #999;
}

JavaScript

function myFunction(){
    var count = 0;
    var WinNum = 100000;
    var rate = document.getElementById('rate').value;
   
    if($.isNumeric(rate)){
        if(rate<1 & rate>0){  
            for (i = 0; i < WinNum; i++) {
            var winRate = Math.random();
                if (winRate >rate) {
                    count++;
                } 
                else {
                    count++;
                    i--;
                }
            }
            document.getElementById("demo").innerHTML =(WinNum / count);
        }
        else{
            alert("rate must between 0 and 1");
        }
    }
    else{
        alert("please input a number");
    }    
}