Skip to content

Instantly share code, notes, and snippets.

@euglv
Last active December 24, 2019 05:15
Show Gist options
  • Save euglv/17baf8dd26ffbb8f97498123bcf54e32 to your computer and use it in GitHub Desktop.
Save euglv/17baf8dd26ffbb8f97498123bcf54e32 to your computer and use it in GitHub Desktop.
Bootstrap Random Game
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>+50% or -40% ?</title>
</head>
<body class="container">
<h1>+50% or -40% ?</h1>
<h5>
Max amount: <span id="max"></span><br>
Current amount: <span id="amount"></span><br>
Total: <span id="total"></span><br>
Minimum total: <span id="minTotal"></span><br>
Iterations: <span id="cnt"></span><br>
Cycles: <span id="cycles"></span><br>
Added: <span id="add"></span><br>
Substracted: <span id="sub"></span><br>
</h5>
<br><br>
<input type="number" id="count" placeholder="count">
<input type="number" id="startAmount" placeholder="startAmount" value="50"> <button id="start">Start</button>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/big.min.js"></script>
<script>
Big.DP=2;
Big.RM=0;
let amount, total, cycles, cnt, a, m, interval, max, minTotal;
$('#start').click( ()=>{
amount = new Big($('#startAmount').val());
total = new Big(0).minus(amount);
minTotal = total;
cycles = 1;
cnt = 0;
m = 0;
a = 0;
max = new Big(0);
if (interval)
clearInterval(interval);
interval = setInterval( () => {
if (Math.random() < 0.5) /*getRandomInt(2) == 0)*/ {
amount = amount.times(1.5);
a++;
} else {
amount = amount.minus(amount.times(0.4));
m++;
}
amount = amount.round(2);
if (amount.gt(max)) {
max = amount;
}
$('#amount').text(amount.toString());
cnt++;
if ( +$('#count').val() && cnt >= +$('#count').val() ) {
cnt = 0;
total = total.plus(amount);
amount = new Big($('#startAmount').val());
total = total.minus(amount);
cycles++;
}
if(total.lt(minTotal)) {
minTotal = total;
}
$('#cnt').text(cnt+1);
$('#add').text(a);
$('#sub').text(m);
$('#cycles').text(cycles);
$('#total').text(total.toString());
$('#max').text(max.toString());
$('#minTotal').text(minTotal.toString());
if (amount.eq(0)) {
if ( +$('#count').val() )
cnt = +$('#count').val()
else
clearInterval(interval);
}
}, 0);
})
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
</script>
</body>
</html>
console.log('Hello World!');
/* todo: add styles */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment