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

0

The task is to measure whether the IQA is good or bad. IQA is a measure of the air quality index. This measurement is made in conjunction with the measurement of two gases: O3, PM10. So you have a table to keep track of if 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 O3 is "bad" the IQA will be "bad". Likewise when PM10 is poor 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 sure how 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>
    
asked by anonymous 15.10.2018 / 20:04

1 answer

1

Your code has some problems:

The first one that is not reading the correct input values (assuming ids are correct) is missing .value on document.getElementById .    document.getElementById returns the element, the control input , to read the value has to use the value property, like this: document.getElementById("O3id").value

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

For your code, it would show "good IQA" because "O3> = 59.4" and would also display "average IQA" because "PM10> = 49.4". So you need to use else .

See 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 did not show how you are running javascript , but the most correct would be to read the values at the time of executing function , so I moved the commands that read values to the variables into the function , to make sure that, when executing, it will pick up the values that are at that moment.

    
15.10.2018 / 20:27