Salary adjustment calculation (according to category) with if Else only returns the first condition of 4. Javascript

Asked

Viewed 28 times

-4

I created a Javascript code with if Else to show on the screen the readjustment of a salary according to the category of worker selected in an input type ="radio". It turns out that only the first condition is calculated, regardless of the category choice by the user. Beforehand, very grateful for the help!

function calcular() {
                var ca = document.getElementById("categoriaA");
                var cb = document.getElementById("categoriaB");
                var ck = document.getElementById("categoriaK");
                var cl = document.getElementById("categoriaL");                
                var x = Number(salario.value);
                
                var novoSalario; 
                var result = document.getElementById ("res");
                result.value = novoSalario;

                if(ca=true){
                    novoSalario = x + 100;
                    result.innerHTML = `Você passa a ganhar R$ ${novoSalario}`;                                      
                }else if(cb=true){
                    novoSalario = x + 150;
                    result.innerHTML = `Você passa a ganhar R$ ${novoSalario}`;                    
                }else if(ck=true){
                    novoSalario = x + 200;
                    result.innerHTML = `Você passa a ganhar R$ ${novoSalario}`;
                }else if(cl=true){
                    novoSalario = x + 250;
                    result.innerHTML = `Você passa a ganhar R$ ${novoSalario}`;
                }else{
                    result.innerHTML = `Você precisa marcar a sua categoria`;
                }                
            }

1 answer

1

Thank you all for your help! Here’s the corrected code:

"" Function calculate() {

            var category = document.getElementsByName("categoria")               
            var x = Number(salario.value);
            
            var novoSalario; 
            var result = document.getElementById ("res");
            result.value = novoSalario;

            if(category[0].checked){
                novoSalario = x + 100;
                result.innerHTML = `Você passa a ganhar R$ ${novoSalario}`;                                      
            }else if(category[1].checked){
                novoSalario = x + 150;
                result.innerHTML = `Você passa a ganhar R$ ${novoSalario}`;                    
            }else if(category[2].checked){
                novoSalario = x + 200;
                result.innerHTML = `Você passa a ganhar R$ ${novoSalario}`;
            }else if(category[3].checked){
                novoSalario = x + 250;
                result.innerHTML = `Você passa a ganhar R$ ${novoSalario}`;
            }else{
                result.innerHTML = `Você precisa marcar a sua categoria`;
            }                
        }
    </script>
</body>
""

Browser other questions tagged

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