Solution 1 :

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">&nbsp0</div>
    </td>
  </tr>
</table>

Problem :

Hello I made a simple math function to show users a calculation of one of my battle functions.
But somehow I cant get it working. I always get 0 no matter what I type in.

Javascript:

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(moon_size) * Math.sqrt((deathstar / (220 * Math.max(1, Math.pow(0.90, moonBase))))), 
                       1
                     ),
                     100), 
                   0
                 )
               );

  $('#moon_destr').text(NumberGetHumanReadable(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">&nbsp0</div>
    </td>
  </tr>
</table>

Comments

Comment posted by t.niese

You should split that whole expression you assign to

Comment posted by Rajesh

This

Comment posted by Maaverick

Still do not work if fix this.

Comment posted by Rajesh

@Maaverick I know that. I even tried to solve it but you need to simplify the formula. Its too complex and parenthesis might cause issues.

Comment posted by Maaverick

Okay, but I am just a beginner with Javascript and I made this function by checking out other function that I have and what work.

Comment posted by Maaverick

It still not working not on JsFiddle not on my website, but thanks for trying out. I am still trying to correct the function

Comment posted by t.niese

@Maaverick well I can’t correct your code as it is not clear what parameters should belong to

Comment posted by Maaverick

I understand that and thank you, I just use the formula what my PHP file is using to calculate the destroy chance and tryed to insert it in javascript, but seems like it will not work that way 😀

By