Calculation with Javascript gives error

Asked

Viewed 62 times

0

Guys, my program does a series of calculations, however there is one that doesn’t work and I no longer have ideas on how to solve it.

The calculations are these:

 function calculaResultado(x){        
        console.log(x);
        a = document.getElementById('avInicial' + x).value;
        b = document.getElementById('meta' + x).value;
        c = document.getElementById('avFinal' + x).value;

        let resultado = ((c*100)/b);

        if(b === c){
            resultado = 100; //100%
        } else if (a > c) {
            resultado = 0; // 0%
        } 
        else {
            resultado = parseInt(resultado);
        }

        document.getElementById('resultado' + x).value =  resultado; 

        calculaMediaFinal();
        ContaObjetivos();
        ContaConcluidos();
        Totalmente();
    }

    function calculaMediaFinal() {
      let soma = 0;
      let contador = 0;
      for (var i = 1; i <= 12; i++) { 
        if (document.getElementById('resultado' + i).value) {
          soma += parseInt(document.getElementById('resultado' + i).value, 10);
          contador++;
        }
      }
      var media = soma / contador;
      var inputCuboMedia = document.getElementById('ConcretizaObj');
      inputCuboMedia.value = parseInt(media, 10);
    }

    function ContaObjetivos(){
        let contador = 0;
        for (var i = 1; i <= 12; i++){
            if (document.getElementById('resultado' + i).value) {
          contador++;
        }
    }var inputCount = document.getElementById('ObjDefinidos');
    inputCount.value = parseInt(contador,10);
    }

    function ContaConcluidos(){
        let contador = 0;
        for (var i = 1; i <= 12; i++){
            if ((document.getElementById('resultado' + i).value) == 100) {
          contador++;
            } 
        }
        var inputAting = document.getElementById('ObjAtingidos');
        inputAting.value = parseInt(contador,10);
    }

    function Totalmente(){
         def = document.getElementById('ObjDefinidos');
         atin = document.getElementById('ObjAtingidos');


        var valor = atin / def;
        var inputdivisao = document.getElementById('TotalAlcancados');
        inputdivisao.value = parseFloat(valor, 10);
    }

The function that does not work is the last: result is: inserir a descrição da imagem aqui

You can see if you can find this Nan’s motive?

1 answer

1


Failed to add .value when selecting the elements:

def = document.getElementById('ObjDefinidos').value;
atin = document.getElementById('ObjAtingidos').value;

The way it is, it’s just selecting the elements themselves, not their values.

  • Thank you so much for the help, it worked :)

Browser other questions tagged

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