You have multiple problems in this expression:
var result = Math.max(
Math.min(
Math.round(
(
100 - Math.sqrt(moon_size) * Math.sqrt((deathstar / (220 * Math.max(1, Math.pow(0.90, moonBase))))),
1
),
100),
0
)
);
In your Math.round
you have:
(
100 - Math.sqrt(moon_size) * Math.sqrt((deathstar / (220 * Math.max(1, Math.pow(0.90, moonBase))))),
1
)
Which is ( … /* that long expression * / … , 1)
do to the comma operator this evaluates always to 1
. So this is essential Math.round(1, 100)
So your code does this:
var result = Math.max(
Math.min(
Math.round(1, 100),
0
)
);
The second argument of round
is ignored so it is Math.round(1)
.
And so the remaining code is: Math.max( Math.min(1, 0) )
, which is always 0
.
You should move at least 100 - Math.sqrt(moon_size) * Math.sqrt((deathstar / (220 * Math.max(1, Math.pow(0.90, moonBase)))))
out of your expression and save it in a variable. And then us that variable in your round
, min
, max
expression.
That way you can see if you have the correct braces and parameters.
So fixing those braces results into this:
function destroyMoon() {
var deathstar = $('#deathstar').val();
var moonBase = $('#moonBase').val();
var moonSize = $('#moonSize').val();
var result = Math.max(
Math.min(
Math.round(
100 - Math.sqrt(moonSize) * Math.sqrt((deathstar / (220 * Math.max(1, Math.pow(0.90, moonBase)))))
),
100),
0
)
;
$('#moon_destr').text(result);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="list listOfOthers hard-table">
<tr>
<td>Check your moon destroy %
<td>
</tr>
<tr>
<td>Moon base level<input type="text" size="10" value="0" name="moonBase" id="moonBase" onkeyup="destroyMoon();"></td>
<td>Deathstars: <input type="text" size="10" value="0" name="deathstar" id="deathstar" onkeyup="destroyMoon();"></td>
<td>Moon size <input type="text" size="10" value="0" name="moonSize" id="moonSize" onkeyup="destroyMoon();"></td>
<td>Moon destroy chance
<div class="text" id="moon_destr"> 0</div>
</td>
</tr>
</table>