Error if condition

Asked

Viewed 60 times

1

I have a condition if that only sends to Him, even when the boolean returned must be true, the condition falls on the else, it must be a silly mistake, but I’ve already reviewed the code and found nothing yet:

if((result1 + result4) > 5189.82){
    result7 = 570.88;
}
else{
    result7 = ((result1 - result4) * 11 / 100).toFixed(2);
}

result1 the user will pass through an input and ``, the whole code:

<script type="text/javascript">
    function apagarUm(){
        form1.bonus.value = "";
    }
    function apagarDois(){
        form1.email.value = "";
    }
    function simular(){
        function isNumber(n) {
            return !isNaN(parseFloat(n)) && isFinite(n);
        }

        //Verifica se tem 'R$' na string e remove.
        if(form1.bonus.value.includes("R$")){
            form1.bonus.value = form1.bonus.value.replace("R$","");
        }

        //Verifica se é um número e maior que 0.
        if(isNumber(form1.bonus.value) == true && form1.bonus.value > 0){
            if(form1.email.value == "" || form1.email.value == "Seu email"){
                alert("Informe o email");
            }
            else{
                //Criando as variáveis e atribuindo os valores.
                var result1 = (parseFloat(form1.bonus.value)).toFixed(2);

                var result2 = result1;

                var result3 = ((result1 - result2) / result1 * 100).toFixed(0);

                var result4 = (3).toFixed(2);

                var result5 = 3.99;

                var result6 = ((result4 - result5) / result4 * 100).toFixed(0);

                var result7;
                var teste = 1;
                if(result1 + result4 > 5189.82){
                    result7 = 570.88;
                }
                else{
                    result7 = ((result1 - result4) * 11 / 100).toFixed(2);
                }

                var result8 = (44).toFixed(2);

                var result9 = ((result7 - result8) / result7 * 100).toFixed(0);

                var receitaTributavelAutonomo = result1 - result4 - result7;
                var irrfAutonomo;
                var irrfDeducao;
                if(receitaTributavelAutonomo < 1904.98){
                    irrfAutonomo = 0;
                    irrfDeducao = 0;
                }
                else if(receitaTributavelAutonomo > 1903.98 && receitaTributavelAutonomo < 2827.65){
                    irrfAutonomo = 7.5;
                    irrfDeducao = 142.8;
                }
                else if(receitaTributavelAutonomo > 2826.65 && receitaTributavelAutonomo < 3752.05){
                    irrfAutonomo = 15;
                    irrfDeducao = 354.8;
                }
                else if(receitaTributavelAutonomo > 3751.05 && receitaTributavelAutonomo < 4665.68){
                    irrfAutonomo = 22.5;
                    irrfDeducao = 636.13;
                }
                else{
                    irrfAutonomo = 27.5;
                    irrfDeducao = 869.36;
                }
                var result10 = (receitaTributavelAutonomo * irrfAutonomo / 100 - irrfDeducao).toFixed(2);

                var rendaBruta = result1 * 12;
                var receitaTributavel = rendaBruta * (1 - (1 * 32 / 100));
                var porc20Simplificado = receitaTributavel * 0.8;
                var irrfMEI;
                var irrfMEIDeducao;
                if(porc20Simplificado < 22847.77){
                    irrfMEI = 0;
                    irrfMEIDeducao = 0;
                }
                else if(porc20Simplificado > 22847.76 && porc20Simplificado < 33919.81){
                    irrfMEI = 7.5;
                    irrfMEIDeducao = 1713.58;
                }
                else if(porc20Simplificado > 33919.80 && porc20Simplificado < 45012.61){
                    irrfMEI = 15;
                    irrfMEIDeducao = 4257.57;
                }
                else if(porc20Simplificado > 45012.60 && porc20Simplificado < 55976.17){
                    irrfMEI = 22.5;
                    irrfMEIDeducao = 7633.51;
                }
                else{
                    irrfMEI = 27.5;
                    irrfMEIDeducao = 10432.32;
                }
                var result11 = ((porc20Simplificado * irrfMEI / 100 - irrfMEIDeducao) / 12).toFixed(2);

                var result12 = ((result10 - result11) / result10 * 100).toFixed(0);
                if(isNumber(result12) == false){
                    result12 = "-";
                }

                var descontaISS = true;
                var result13;
                if(descontaISS == true){
                    result13 = (result1 * 5 / 100).toFixed(2);
                }
                else{
                    result13 = (0).toFixed(2);
                }

                var result14 = (5).toFixed(2);

                var result15 = ((result13 - result14) / result13 * 100).toFixed(0);
                if(isNumber(result15) == false){
                    result15 = "-";
                }

                var result16 = (result1 - result4 - result7 - result10 - result13).toFixed(2);

                var result17 = (result2 - result5 - result8 - result11 - result14).toFixed(2);

                var result18 = ((result17 - result16) / result16 * 100).toFixed(0);

                var result19 = (result17 - result16).toFixed(2);

                //Imprimindo os resultados.
                var taxaSemMOBI = document.getElementById("taxaSemMOBI");
                var taxaComMOBI = document.getElementById("taxaComMOBI");
                var inssSemMOBI = document.getElementById("inssSemMOBI");
                var inssComMOBI = document.getElementById("inssComMOBI");
                var irrfSemMOBI = document.getElementById("irrfSemMOBI");
                var irrfComMOBI = document.getElementById("irrfComMOBI");
                var issSemMOBI = document.getElementById("issSemMOBI");
                var issComMOBI = document.getElementById("issComMOBI");
                var bonusLiquidoSemMOBI = document.getElementById("bonusLiquidoSemMOBI");
                var bonusLiquidoComMOBI = document.getElementById("bonusLiquidoComMOBI");
                var valor = document.getElementById("valor");


                taxaSemMOBI.innerHTML = "R$ " + result4;
                taxaComMOBI.innerHTML = "R$ " + result5;
                inssSemMOBI.innerHTML = "R$ " + result7;
                inssComMOBI.innerHTML = "R$ " + result8;
                irrfSemMOBI.innerHTML = "R$ " + result10;
                irrfComMOBI.innerHTML = "R$ " + result11;
                issSemMOBI.innerHTML = "R$ " + result13;
                issComMOBI.innerHTML = "R$ " + result14;
                bonusLiquidoSemMOBI.innerHTML = "R$ " + result16;
                bonusLiquidoComMOBI.innerHTML = "R$ " + result17;
                valor.innerHTML = "R$ " + result19;
            }
        }
        else{
            alert("Informe uma quantia válida!");
        }
    }
 </script>
  • You tested what the result of (result1 + result4)? Put a console on to see.

1 answer

4


The problem is that you are comparing strings when you think you are comparing numbers.

The method .toFixed() returns a String. So when you do it for example:

(3).toFixed(2) + (3).toFixed(2) each of the parties will give a string "3.00" and the sign + will concatenate to a new string: 3.003.00... and then the comparison is basically:

"3.003.00".length > String(5189.82).length
// ou seja:
8 > 7

Because you use the .toFixed()? if it’s for humans to read better just put it at the end when you insert it into HTML. If you want to round to 2 decimal places you can do: Math.round(num * 100) / 100.

  • 1

    Thanks Sergio, without words...

Browser other questions tagged

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