I will show an error on the screen, NaN. Help please, I don’t know how to handle it.

/* демо вычисления */ $(function(){ $("#demo7").click(function(){ var m1 = $(".m1").attr("data"); var Pup = $(".pup").attr("data"); var Pzh = $(".pzh").attr("data"); var p = (1+m1)*Pup*Pzh/(Pzh+m1*Pup); if (isNaN(p)) { //производим проверку при делении на нуль, p = 0; //чтобы результат возвращалось бы не NaN, а число 0 } var Ptc=p.toFixed(3); //округлим результат в Демо до десятых var V = $(".v").attr("data"); var G=(Ptc*V)/(1+m1); var Gtc=G.toFixed(2); $(".m1").val(m1); $(".pup").val(Pup); $(".pzh").val(Pzh); $(".p").val(Ptc); $(".v").val(V); $("#g").val(Gtc); }); }); <form name="forma1" style="background-color:#44944A;"> <!-- поменяю цвет формы --> <div id="resizable"> <br> <!--"подрасчёт 3.3"--> <table align="center"> <p align="center">Количество сухого тампонажного цемента</p> <tr><td height="40px"> <div class="price">Жидкостно-цементное отношение;</div><input name="t26" id="t29" type="text" align="right" size="4" maxlength="8" onkeyup="return proverka(this)" class="m1" data="0.5"> </td> </tr> <tr><td height="40px"> <div class="price">Плотность цемента т/м<sup>3</sup>.</div><input name="t27" id="t29" type="text" align="right" size="4" maxlength="8" onkeyup="return proverka(this)" class="pup" data="3.15"> </td> </tr> <tr><td height="40px"> <div class="price">Плотность жидкости затворения, т/м<sup>3</sup>.</div><input name="t28" id="t29" type="text" align="right" size="4" maxlength="8" onkeyup="return proverka(this)" class="pzh" data="1.08"> </td> </tr> <tr><td height="40px"> <div class="price">Плотность тампонажного раствора;</div><input name="t29" id="t29" type="text" align="right" size="4" maxlength="8" onkeyup="return proverka(this)" class="p"> </td> </tr> <tr><td height="40px"> <div class="price">Общий объем тампонажного раствора;</div><input name="t30" id="t30" type="text" align="right" size="4" maxlength="8" onkeyup="return proverka(this)" class="v" data="2.828"> </td> </tr> <tr><td> <input type="button" name="button" value="Вычислить" onClick="areaRectangle8();"> <input type="button" name="button" value="Вычислить Демо" id="demo7"> <input type="text" name="res8" id="g" size="10"> </td></tr> </table> </div> <br> </form> 

enter image description here

Okay, so I myself fixed my script, and there it was not parseInt that best suited, but parseFloat

Corrected script

  /* демо вычисления */ $(function(){ $("#demo7").click(function(){ var m1 = parseFloat($(".m1").attr("data")); var Pup = parseFloat($(".pup").attr("data")); var Pzh = parseFloat($(".pzh").attr("data")); var p = (1+m1)*Pup*Pzh/(Pzh+m1*Pup); var Ptc=p.toFixed(3); //округлим результат в Демо до десятых var V = $(".v").attr("data"); var G=(Ptc*V)/(1+m1); var Gtc=G.toFixed(2); $(".m1").val(m1); $(".pup").val(Pup); $(".pzh").val(Pzh); $(".p").val(Ptc); $(".v").val(V); $("#g").val(Gtc); }); }); 

And the correct calculations, and with parseInt, it turned out that "

Corrected calculations Thank you for trying to help.

Closed due to the fact that off-topic participants Dmitriy Simushev , user194374, aleksandr barakin , Streletz , Grundy 19 May '16 at 11:37 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Dmitriy Simushev, Community Spirit, aleksandr barakin, Streletz, Grundy
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • if you want to add a question: use the edit button questionable - Grundy
  • one
    read more about debugging the code , at least about outputting intermediate values ​​to the console, in the future it will help you look for errors and fix them. - Alex Krass

2 answers 2

The picture shows that one of the values ​​is NaN , respectively, all subsequent operations with this value will also return NaN .

Add a check that the current value is not NaN before using this value.

    The value of NaN indicates that as a result of the calculations you did not receive a number, but something incomprehensible. For example, if you multiply the word "fridge" by the number 5, then as a result you will not know what.

    Here you have the same thing, you get strings through the function $ ("*"). Attr ("data") and try to multiply them and add them to each other. By the way, the addition in your case is also incorrect, because "1" + "0.5" = "10.5". And I think this is not what you expect.

    Use conversion to numbers before math operations.

     var m1 = parseInt($(".m1").attr("data"),10); var Pup = parseInt($(".pup").attr("data"),10); var Pzh = parseInt($(".pzh").attr("data"),10); var V = parseInt($(".v").attr("data"),10);