salbatzaile
salbatzaile

Reputation: 13

Calculate with formula javascript

Lately i want to make a converter formula with javascript. but i got a stack at this function

javascript

function UconverterDP(){
            var inputD = document.getElementById('inputD').value;
            var inputP = document.getElementById('inputP').value;
            jikaP = inputD * 0.4;
            bagianatasU1 = 0.8 * inputP * inputD;
            bagianbawahU1 = P + (0.4 * inputD);
            hasilU1 = bagianatasU1 / bagianbawahU1;
            bagaianatasU2 = 0.8 * inputD;
            bagaianatasU2a =  1 - bagaianatasU2 ;                                       
            bagaianatasU2h = inputP - bagaianatasU2a;
            bagianbawahU2 = 0.0114 * inputP;
            bagianbawahU2a = Math.pow(bagianbawahU2, 1.7);
            bagianbawahU2b = 0.92 * bagianbawahU2a;
            bagianbawahU2h = bagianbawahU1 * bagianbawahU2b;
            hasilU2 = bagaianatasU2h / bagianbawahU2h;
            if (inputP <= jikaP) {
            document.getElementById("outputU").innerHTML = Hasil U1;    
            } else {
            document.getElementById("outputU").innerHTML = hasilU2;
            }
        }   

this my input

<tr>
    <td>
        <label>D : </label>
        <input id="inputD" type="number" onchange="UconverterDP()">
    </td>
    <td>
        <label>P : </label>
        <input id="inputP" type="number" onchange="UconverterDP()"  >
    </td>
</tr>

and this output

<label> BUI (U): </label>
<span id="outputU"></span>

And this is the formula:

enter image description here

Upvotes: 0

Views: 1495

Answers (1)

mrpepo877
mrpepo877

Reputation: 542

i really don t know if this is what are you searching, if it isn't tell me what is missing (and explain me a little bit more your idea):

<script>

function UconverterDP(){
   var inputD = document.getElementById('inputD').value;
   var inputP = document.getElementById('inputP').value;
   jikaP = inputD * 0.4;
   if (inputP <= jikaP) { // here your program choose what formula will use
      var u =(0.8*inputP*inputD)/(inputP+0.4*inputD)// here is the first formula
      document.getElementById("outputU").innerHTML = u;// and then here you return the value that you want    
    } 
   else {
      var u = inputP-((1-0.8*inputD)/(inputP+0.4*inputD))/(0.92*0.0114*(inputP**1.7))// and here is the second formula
      document.getElementById("outputU").innerHTML = u;// here is the same you return the value
    }
}   
// maybe you can do more short this code putting together the two "document.getElementById("outputU").innerHTML = u;" here, but work in this way also
</script>

Upvotes: -1

Related Questions