función javascript que no funciona pero no muestra ningún error

I've done a function in javascript in roder to calcuate amount of fees for a advocate cabinet.

They ask me to do this, in fact it is proportional to the amount of the file. and it is degressif.

the more is the amount then the percentage will be less, but for each .

In fact When I have no initial payment it calculate fine. when I have a payment registered it does not display anything And I have no error mistakes in the console.

here is the function I've done.

<script type="text/javascript">

function hono(p1,p2,p3,p4,t1,t2,t3,type,versement,montantacompte)
     {
         var p1             = 5000.00;
         var p2             = 5000.01;
         var p3             = 8000.00;
         var p4             = 8000.01;
         var t1             = 17.00;
         var t2             = 10.00;
         var t3             = 6.00;
         var type           = "d";
         var versement      = 150.00;
         var montantacompte = document.getElementById("montantacompte").value;


         if (type== "d")
            {
                if(versement== 0.00 && montantacompte <= p1)
                {
                    reste1= montantacompte;
                    pc= t1/100;
                    taux1= pc+1;
                    h1=((reste1*taux1)-reste1);
                    honoraires=h1;
                    document.getElementById('taux-1').value=t1;
                    document.getElementById('palier1').value=Math.round(h1*100)/100;
                    document.getElementById('sommehonoraires').value=Math.round(honoraires*100)/100;
                }
                else if(versement < p1 && (versement + montantacompte) <= p1)
                {
                    reste1= montantacompte;
                    pc= t1/100;
                    taux1= pc+1;
                    h1= (reste1*taux1)-reste1;
                    honoraires= h1;
                    document.getElementById('taux-1').value=t1;
                    document.getElementById('palier1').value=Math.round(h1*100)/100;
                    document.getElementById('sommehonoraires').value=Math.round(honoraires*100)/100;
                }
                else if(versement <= p1 && (versement+montantacompte)>= p2 && (versement+montantacompte)<=p3)
                {
                    reste1=p1-versement;
                    pc=t1/100;
                    taux1=pc+1;
                    h1=(reste1*taux1)-reste1;
                    reste2=montantacompte-reste1;
                    pc2=t2/100;
                    taux2=pc2+1;
                    h2=(reste2*taux2)-reste2;
                    honoraires=h1+h2;
                    document.getElementById('taux-1').value=t1;
                    document.getElementById('palier1').value=Math.round(h1*100)/100;
                    document.getElementById('taux-2').value=t2;
                    document.getElementById('palier2').value=Math.round(h2*100)/100;
                    document.getElementById('sommehonoraires').value=Math.round(honoraires*100)/100;
                }
                else if (versement==0.00 && montantacompte>=p2 && montantacompte <=p3)
                {
                    reste2=montantacompte-p1;
                    reste1=p1;
                    pc=(t1/100);
                    taux1=pc+1;
                    h1=((reste1*taux1)-reste1);
                    pc2=(t2/100);
                    taux2=pc2+1;
                    h2=((reste2*taux2)-reste2);
                    honoraires=(h1+h2);
                    document.getElementById('taux-1').value=t1;
                    document.getElementById('palier1').value=Math.round(h1*100)/100;
                    document.getElementById('taux-2').value=t2;
                    document.getElementById('palier2').value=Math.round(h2*100)/100;
                    document.getElementById('sommehonoraires').value=Math.round(honoraires*100)/100;
                }
                else if(versement<=p3 && versement>=p2 && (versement+montantacompte)>=p2 && (versement+montantacompte)<=p3)
                {
                reste1=montantacompte;
                pc=(t2/100);
                taux1=pc+1;
                h1=((reste1*taux1)-reste1);
                honoraires=h1;  
                document.getElementById('taux-1').value=t1;
                document.getElementById('palier1').value=Math.round(h1*100)/100;
                document.getElementById('sommehonoraires').value=Math.round(honoraires*100)/100;
                }
                else if(versement>=p2 && versement<=p3 && (versement+montantacompte)>=p4)
                {
                reste1=p4-versement;
                pc=(t2/100);
                taux2=pc+1;
                h2=((reste1*taux2)-reste1);
                reste2=montantacompte-reste1;
                pc2=(t3/100);
                taux3=pc2+1;
                h3=((reste2*taux3)-reste2);
                honoraires=(h2+h3);
                document.getElementById('taux-3').value=t3;
                document.getElementById('palier3').value=Math.round(h3*100)/100;
                document.getElementById('taux-2').value=t2;
                document.getElementById('palier2').value=Math.round(h2*100)/100;
                document.getElementById('sommehonoraires').value=Math.round(honoraires*100)/100;
            }
            else if(versement==0.00 && montantacompte>=p4)
            {
                reste1=p1;
                reste2=p3-p2;
                pc=(t1/100);
                taux1=pc+1;
                h1=((reste1*taux1)-reste1);
                pc2=(t2/100);
                taux2=pc2+1;
                h2=((reste2*taux2)-reste2);
                reste3=(montantacompte-reste1-reste2);
                pc3=(t3/100);
                taux3=pc3+1;
                h3=((reste3*taux3)-reste3);
                honoraires=(h1+h2+h3);
                document.getElementById('taux-1').value=t1;
                document.getElementById('palier1').value=Math.round(h1*100)/100;
                document.getElementById('taux-2').value=t2;
                document.getElementById('palier2').value=Math.round(h2*100)/100;
                document.getElementById('taux-3').value=t3;
                document.getElementById('palier3').value=Math.round(h3*100)/100;
                document.getElementById('sommehonoraires').value=Math.round(honoraires*100)/100;
            }
        else if(versement>=p4)
            {
                reste3=montantacompte;
                pc3=(t3/100);
                taux3=pc3+1;
                h3=((reste3*taux3)-reste3);
                honoraires=(h3);

                document.getElementById('taux-3').value=t3;
                document.getElementById('palier3').value=Math.round(h3*100)/100;
                document.getElementById('sommehonoraires').value=Math.round(honoraires*100)/100;
            }
        }
        if (type=="l")
        {
        pc1=(t1/100);
        taux1=pc1+1;
        honoraires=montantacompte*taux1;
        document.getElementById('taux-1').value=t1;
        document.getElementById('palier1').value=Math.round(h1*100)/100;
        document.getElementById('sommehonoraires').value=Math.round(honoraires*100)/100;
        }
    </script>

Moreover all function use the case if(versement== 0.00 && montantacompte <= p1) and after, and so on else if(versement < p1 && (versement + montantacompte) <= p1) hasta el final.

I don't understand why it does not work.

preguntado el 28 de agosto de 12 a las 08:08

This is a lot of code to try and understand simply by reading it. Is it possible to replicate it using jsfiddle.net and give examples of exactly what the problem is (with values)? -

¿Por qué estás declarando? p1, p2, etc. as function parameters and then also declaring them with var and setting their values? -

are you missing by mistake a closing } al final de la función? -

here is the egzample. jsfiddle.net/KUMSG in fact all is in the function hon() when you look at the var versement when it is at 0 all works fine . when you write an anmount in the input called montantacompte. It works fine in the table below it calculate the feees. but when the var versement is not equal to zero in the javascript it dfoes not work more. I do not understand why -

I'm not sure comparing float values like this versement==0.00 is the best idea considering that floats are not super accurate...If you're working with money think about storing wholes and cents in separate objects. -

1 Respuestas

Try to "fix" that by replacing:

var montantacompte = document.getElementById("montantacompte").value;

by

var montantacompte = parseFloat(document.getElementById("montantacompte").value);

It seems that there's a mess with types - when you get the field value, it is string so any other operations (like (versement+montantacompte) ) will be done with strings, not with numbers.

Respondido 28 ago 12, 09:08

Oh thanks because of you it works fine. I don't know why It has stop works, because UI used that code before and it worked fine. Thank you verry muche for your help, it works fine. do you think it is better for me to change all var using that code? - Estanislao Piotrowski

For sure, yes, if they're coming from user inputs. If they're constants and are numeric constants - you don't need to change them. - Denis Mazourick

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.