I’m trying to write a JS code to test the value of "Very Good" to "Bad " in IQA(Air Quality Index)

Asked

Viewed 30 times

0

The task consists of the following, measuring whether the IQA is good or bad. IQA is a measure of the Air Quality Index. This measurement is done in conjunction with the measurement of two gases: O3, PM10. This way has a table to monitor whether the index is good or bad.

              O3          PM10
Índice       Maximo      Maximo

Muito Bom       59.4        19.4

Bom             119.4       34.4

Medio           179.4       49.4

Fraco           239.4       119.4

Ruim           >239.5      >119.5

NOTE: The IQA will be determined by the worst index. When the O3 is "bad" the IQA will be "bad". Similarly when the PM10 is bad the IQA will be "bad". The worst index will determine the IQA

Here is my code. However my doubt is that this code works but it always writes that it is "VERY GOOD" and I am not able to fix it.

   <head>

       <title>CalculoDeIQA</title>

       <meta charset="UTF-8"/> 

   </head>
       <body>
           <h1>Forneça o indice de PM10 e 03 para o cálculo do IQA </h1>
       PM10:<input type="text" name="PM10" id="PM10id">
   <br>
       O3: <input type="text" name="O3" id="O3id" >
   <br>
   <br>
       Enviar: <input type="submit" name="enviar" onclick="calculaIqa()">


        <script language="JavaScript">
        var O3=document.getElementById("O3id");
        var PM10=document.getElementById("PM10id");
        function calculaIqa(){

            if((O3>=239.5) ||  (PM10>=119.5)){

                document.write("IQA ruim ");
            }
            if((O3>=239.4) || (PM10>=119.4)){

                documento.write("IQA fraco");
            }
            if(O3>=179.4||PM10>=49.4){

                document.write("IQA medio");
            }
            if((O3>=119.4)||(PM10>=34.4)){

                document.write("medio");
            }
            if((O3>=59.4) || (PM10>=19.4)){ 
                document.write("IQA  bom");

            }else{
                document.write("IQA Muito Bom");
            }



        }
        </script>
    </body>
</html>

1 answer

1


Your code has some problems:

The first one who is not reading the correct input values (from the idea that the ids are correct) is that it was missing .value in the document.getElementById. document.getElementById returns the element, the control input, to read the value you have to use the value property, so: document.getElementById("O3id").value

Second, the way the if, each line is one if isolated, but you want to evaluate so "be it, else, if it is that, else, if it is that, etc.". For this, you must include the else. Otherwise, a condition can meet at the same time more than one if and show more than one result.
To test, think of the values O3=90 and PM10=90. How you are using the operator "or" ||, any of the conditions would be accepted correctly?

By its code, it would show "good IQA", because "O3>=59.4" and would also show "average IQA", because "PM10>=49.4". That’s why you need to use the else.

Take this example:

document.getElementById("bt").addEventListener("click", function(){
    calculaIqa();
});

  function calculaIqa() { 

      var O3=(document.getElementById("O3id").value);
      var PM10=(document.getElementById("PM10id").value);


      if((O3>=239.5) || (PM10>=119.5)){

          document.write("IQA ruim ");
      }
      else if((O3>=239.4) || (PM10>=119.4)){

          documento.write("IQA fraco");
      }
      else if(O3>=179.4||PM10>=49.4){

          document.write("IQA medio");
      }
      else if((O3>=119.4)||(PM10>=34.4)){

          document.write("medio");
      }
      else if((O3>=59.4) || (PM10>=19.4)){ 
          document.write("IQA  bom");

      }else{
          document.write("IQA Muito Bom");
      }
 }
<p>
  O3: 
  <input type="text" id="O3id" />
</p>
<p>
  PM10: 
  <input type="text" id="PM10id" />
</p>
<p>
    <button id="bt">
    Calcular
    </button>
</p>

Finally, you have not shown how you are performing the javascript, but the most correct would be to read the values when executing the Function, so I moved the commands that read the values for the variables into the Function, to make sure that, when executing, will take the values that are in that moment.

  • Could you explain to me what this line is in the code ? Document.getElementById("Bt"). addeventlistener("click", Function(){ calculaIqa(); });

  • 1

    yes, it is to associate the event "onclick" on the button with id "Bt", for example <button id="bt" onclick="calculalqa()">. Is that I tested the code first on the site http://jsfiddle.net/ and there this second way does not work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.