javascript - Calculation keeps giving a NaN result -


i given formula below:

weightdifference = (|crushedweight - weightba| / weightba )* 100% 

where weightba , crushedweight values of input texts. did make function stated below:

function caldiffweight() {     var weightds = document.getelementbyid("weightds");     var crushedweight = document.getelementbyid("crushedweight");     var abs = -1;     var diffweight = ((crushedweight - weightds) * abs) / weightds * 100;     document.getelementbyid("diffweight").value = diffweight; } 

and absolute calculation in |crushedweight - weightba| how should it?

based on code made following changes:

  • you elements weightds , crushedweight never got values inputs;
  • converted values floating number before using them calculation
  • use math.abs() make number absolute.

const    form = document.getelementbyid('form');      form.addeventlistener('submit', onformsubmit);    function onformsubmit() {    event.preventdefault();    caldiffweight();  }  function caldiffweight() {      var weightds = parsefloat(document.getelementbyid("weightds").value);      var crushedweight = parsefloat(document.getelementbyid("crushedweight").value);      var diffweight = (math.abs(crushedweight - weightds)) / weightds * 100;      document.getelementbyid("diffweight").value = diffweight;  }
<form id="form">    <label>      weightds:      <input type="number" id="weightds"/>    </label>    <label>      crushed weight:      <input type="number" id="crushedweight"/>    </label>        <button>calculate</button>  </form>    <p>result</p>  <input type="number" id="diffweight"/>


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -