Javascript does not pull results

Asked

Viewed 78 times

0

Good afternoon, you guys, I am trying to make you calculate again using other types of field, such as checkbox and list with some conditions, in some tutorials I tried to understand how it works, but I could not make generate the result, if you can take a look and guided thank.

<script type="text/javascript">
function calcular(oper){
    var tributacao = document.mensalidade.tributacao.value;
    var qt_func = document.mensalidade.qt_func.value;
    var fat_mes = document.mensalidade.fat_mes.value;
    var tipo_emp = document.mensalidade.tipo_emp.value;

    var qt_func2 = qt_func * 20;

    alert (qt_func2);

    var soma = parseInt(qt_func2) + parseFloat(tributacao) + parseInt(prestacao) + parseInt(industrial) + parseInt(comercial) ; 

    if (fat_mes <= 25000){
        var res = parseInt(0) + parseFloat(soma);
    } else (fat_mes > 25000 && fat_mes <= 30000){
        var res = parseFloat(soma) + 115;
    } else (fat_mes > 30000 && fat_mes <= 40000){
        var res = parseFloat(soma) + 150;
    } else (fat_mes > 40000 && fat_mes <= 50000){
        var res = parseFloat(soma) + 190;
    } else (fat_mes > 50000 && fat_mes <= 60000){
        var res = parseFloat(soma) + 230;
    } else (fat_mes > 60000 && fat_mes <= 70000){
        var res = parseFloat(soma) + 270;
    } else (fat_mes > 70000 && fat_mes <= 80000){
        var res = parseFloat(soma) + 310;
    } else (fat_mes > 80000 && fat_mes <= 90000){
        var res = parseFloat(soma) + 350;
    } else (fat_mes > 90000 && fat_mes <= 100000){
        var res = parseFloat(soma) + 390;
    } else (fat_mes > 100000 && fat_mes <= 110000){
        var res = parseFloat(soma) + 430;
    } else (fat_mes > 110000 && fat_mes <= 120000){
        var res = parseFloat(soma) + 470;
    } else (fat_mes > 120000){
        var res = (parseFloat(soma) * 0.40) / 100
    }

    document.mensalidade.res.value = res;
}
</script>

<form name="mensalidade" method="post" action="">
    <label>Tributação</label><br>
        <select id="tributacao" name="tributacao" class="form-control input">
            <option value="0">Escolha</option>
            <option value="49.90">MEI</option>
            <option value="219">Simples Nacional</option>
            <option value="319">Lucro Presumido</option>
            <option value="2650">Lucro Real</option>
        </select><br><br>
    <label>Quantidade de Funcionarios</label><br>
    <input type="text" name="qt_func" id="qt_func"><br><br>
    <label>Faturamento mes</label><br>
    <input type="text" name="fat_mes" id="fat_mes"><br><br>
    <label>Selecione os Tipos</label><br>
    <input type="checkbox" name="tipo_emp" id="tipo_emp" value="30">Serviços
    <input type="checkbox" name="tipo_emp" id="tipo_emp" value="150">Industrial
    <input type="checkbox" name="tipo_emp" id="tipo_emp" value="80">Comercial<br><br>
    <input type="text" name="res" id="res"><br><br>
    <input type="button" value="Calcular" onClick="calcular(document.mensalidade.fat_mes.value)"/>
</form>
  • I don’t understand, what should happen? Please explain better

  • it should multiply qt_func by 20 and add the rest of the fields and go through the variable, add the value inside, but it is not showing in the res field...

  • 1

    All the else are wrong, should be else if. The latter could be shortened to } else { (omitting the comparison condition, if justified)

  • It is well explained, with the pardon of the word, I suppose that the "res" you refer is the element <input id="res">. The way you write people have to work harder to understand what you wrote and what you want than to understand where your code fails, that is to say people spend a lot of time trying to understand you instead of spending time trying to find the problem of your code. Please understand as a constructive criticism.

  • @Luishenrique well noticed, I had not even looked at the code because the question itself was hard to understand, good +1

  • @Luishenrique actually the last else this wrong too. Everyone is wrong.

  • In fact the question is very confused, I did not understand her either, but just to hit the code gave to realize the error (not that it is only this). I corrected the last remark else.

  • Sorry, but I’m new to javascript and I’m trying to explain the way I see, I know it’s something simple, but I’ve been killing myself for about 4 days, hunting and trying to do the code.

  • @Viniciusnakamura no one is complaining about your code or your understanding of js, I’m criticizing the way you wrote it yourself, but now it’s a little clearer the question after talking, however it’s a typo (off-topic), I’ll even answer, I hope it helps.

  • I am only apologizing, relax ;) because I am trying to learn javascript and ta being a fight. rs

Show 5 more comments

2 answers

2


you even started with the right foot, isolated the method to calculate in a function.

However I advise not to declare inline events in HTML itself, preferably use HtmlElement.addEventListener(name, callback).

finally, although there are no problems in using Htmlcollection to access the page’s DOM elements in your example, you may encounter some compatibility issues in the future, so I advise using the document.getElementById.

//estou assumindo que você definiu estas variaveis em outro ponto do codigo.
var prestacao = 0;
var industrial = 0;
var comercial = 0;

var mensalidade = {};
mensalidade.form = document.querySelector("[name='mensalidade']");
mensalidade.tributacao = mensalidade.form.querySelector("[name='tributacao']");
mensalidade.qt_func = mensalidade.form.querySelector("[name='qt_func']");
mensalidade.fat_mes = mensalidade.form.querySelector("[name='fat_mes']");
mensalidade.tipo_emp = mensalidade.form.querySelectorAll("[name='tipo_emp']");
mensalidade.calcular = mensalidade.form.querySelector("[name='calcular']");
mensalidade.res = mensalidade.form.querySelector("[name='res']");
Object.defineProperty(mensalidade, "tipo_emp_sum", {
  get: function () {
    return [].reduce.call(mensalidade.tipo_emp, function (valor, tipo_emp, indice) {
      if (tipo_emp.checked)
        valor += parseInt(tipo_emp.value);
      return valor;
    }, 0);
  }
});


function calcular(event){
  var tributacao = parseFloat(mensalidade.tributacao.value) || 0;
  var qt_func = parseInt(mensalidade.qt_func.value) || 0;
  var fat_mes = parseInt(mensalidade.fat_mes.value) || 0;  
  var tipo_emp = mensalidade.tipo_emp_sum;
  var qt_func2 = qt_func * 20;

  var soma = qt_func2 + tributacao + tipo_emp + parseInt(prestacao) + parseInt(industrial) + parseInt(comercial) ; 
  var res = soma;
  switch(true) {
    case (fat_mes <= 25000): break;
    case (fat_mes <= 30000): res += 115; break;
    case (fat_mes <= 40000): res += 150; break;
    case (fat_mes <= 50000): res += 190; break;
    case (fat_mes <= 60000): res += 230; break;
    case (fat_mes <= 70000): res += 270; break;
    case (fat_mes <= 80000): res += 310; break;
    case (fat_mes <= 90000): res += 350; break;
    case (fat_mes <= 100000): res += 390; break;
    case (fat_mes <= 110000): res += 430; break;
    case (fat_mes <= 120000): res += 470; break;
    default: res = (res * 0.40) / 100; break;
  }
  mensalidade.res.value = res;
}

//adicionando os eventos aos inputs.
mensalidade.calcular.addEventListener("click", calcular);
mensalidade.tributacao.addEventListener("change", calcular);
mensalidade.qt_func.addEventListener("input", calcular);
mensalidade.fat_mes.addEventListener("input", calcular);
[].forEach.call(mensalidade.tipo_emp, function (tipo_emp, indice) {
  tipo_emp.addEventListener("change", calcular);
})
<form name="mensalidade" method="post" action="">
    <label>Tributação</label><br>
        <select id="tributacao" name="tributacao" class="form-control input">
            <option value="0">Escolha</option>
            <option value="49.90">MEI</option>
            <option value="219">Simples Nacional</option>
            <option value="319">Lucro Presumido</option>
            <option value="2650">Lucro Real</option>
        </select><br><br>
    <label>Quantidade de Funcionarios</label><br>
    <input type="text" name="qt_func" id="qt_func"><br><br>
    <label>Faturamento mes</label><br>
    <input type="text" name="fat_mes" id="fat_mes"><br><br>
    <label>Selecione os Tipos</label><br>
    <input type="checkbox" name="tipo_emp" id="tipo_emp" value="30">Serviços
    <input type="checkbox" name="tipo_emp" id="tipo_emp" value="150">Industrial
    <input type="checkbox" name="tipo_emp" id="tipo_emp" value="80">Comercial<br><br>
    <input type="text" name="res" id="res"><br><br>
    <input type="button" value="Calcular" name="calcular" "/>
</form>

  • cool, I’m still trying to understand the code you did, I just have to change now select the types to checkbox, because it can choose more than 1 service in the case. would be the same scheme as radio?

  • @Viniciusnakamura, will not break the code, but do not think it will generate the expected result, I believe you will have to make a small adaptation, as replace the filter (picks the first selected) by a reduce (add all the selected values). but I don’t see you using the value of tipo_emp in your script.

  • In case the typo_emp will say the segment of the company, then it would need to add them tbms, if they were selected, I made this change here, but did not add the difference in the final value.

  • @Viniciusnakamura, I made a change to add all the values of tipo_emp and add to the soma.

  • I performed some tests and realized that in the default line: res = (res * 0.40) / 100; break; it wouldn’t have to be fat_mes, because I noticed that it makes a totally different value, in which case it would be 1000, it appears 0.12 adding the value of 250000

  • I also did not understand this line, was completely out of tune with the rest of the code, in any case just transcribed its logic.

  • I did some tests, and I changed a line that I will change in the code, which worked cool, or better added the sum that was like "default:;"

Show 2 more comments

0

Okay, let’s see if I can help you. The if's are wrong, they have to stay just like in this example below and start the variable res before entering us if's so you don’t have to start her all the time.

var res =0;
if (fat_mes <= 25000){
     res = parseInt(0) + parseFloat(soma);
} else if (fat_mes > 25000 && fat_mes <= 30000){
     res = parseFloat(soma) + 115;
} else if (fat_mes > 30000 && fat_mes <= 40000){
     res = parseFloat(soma) + 150;
} else if (fat_mes > 40000 && fat_mes <= 50000){
     res = parseFloat(soma) + 190;
} else if (fat_mes > 50000 && fat_mes <= 60000){
     res = parseFloat(soma) + 230;
} else if (fat_mes > 60000 && fat_mes <= 70000){
     res = parseFloat(soma) + 270;
} else if (fat_mes > 70000 && fat_mes <= 80000){
     res = parseFloat(soma) + 310;
} else if (fat_mes > 80000 && fat_mes <= 90000){
     res = parseFloat(soma) + 350;
} else if (fat_mes > 90000 && fat_mes <= 100000){
     res = parseFloat(soma) + 390;
} else if (fat_mes > 100000 && fat_mes <= 110000){
     res = parseFloat(soma) + 430;
} else if (fat_mes > 110000 && fat_mes <= 120000){
     res = parseFloat(soma) + 470;
} else if (fat_mes > 120000){
     res = (parseFloat(soma) * 0.40) / 100
}

doing so, the error will disappear, but still has the variable prestacao that is not defined anywhere.

  • at the end of the file set an Alert with the variable res and above another with the variable sum, in both do not appear the value.

Browser other questions tagged

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