1
Friends, I am learning to program and I started with Javascript.
I’m having difficulties. My code is not returning the function value calcIrpf().
The code is not complete. The goal is to calculate the net salary according to the CLT. More steps are missing. But I’m blocked on the return of the function.
<meta charset="utf-8">
<h1>Cálculo de Salário</h1>
<p><br>Informe seu salário</p>
<input/>
<button>Calcular</button>
<script>
var input = document.querySelector("input");
input.focus();
function calcIrpf(){
var salario = input.value;
var alIrpf = [0.075, 0.15, 0.225, 0.275];
var parcDedut = [142.8, 354.8, 636.13, 869.36];;
if (salario <= 1903.98){
var valIrpf = 0;
}else{
if (salario >1903.98 && salario <=2826.65) {
valIrpf = (salario*alIrpf[0])-parcDedut[0];
}else{
if (salario > 2826.65 && salario <= 3751.05) {
valIrpf = (salario*alIrpf[1])-parcDedut[1];
}else{
if(salario > 3751.05 && salario <= 4664.68){
valIrpf = (salario*alIrpf[2])-parcDedut[2];
}else{
if(salario < 4664.68){
valIrpf = (salario*alIrpf[3])-parcDedut[3];
}
}
}
}
}
return valIrpf;
}
function mostra(){
document.write("<br>O valor de IRPF descontado é: R$"+calcIrpf());
}
var button = document.querySelector("button");
button.onclick = mostra;
</script>
It was not enough to ask the main question: the problem.
– Sam
missing ; in Return valIrpf
– Amadeu Antunes
Amadeu Antunes -> Corrected ; but did not fix.
– G.Moric
Sam -> I corrected the question
– G.Moric
In that last if, the signal should be bigger and not smaller:
if(salario < 4664.68){... would beif(salario > 4664.68){– Sam