Code returns Undefined when executed

Asked

Viewed 45 times

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.

  • missing ; in Return valIrpf

  • Amadeu Antunes -> Corrected ; but did not fix.

  • Sam -> I corrected the question

  • In that last if, the signal should be bigger and not smaller: if(salario < 4664.68){... would be if(salario > 4664.68){

1 answer

2

At last if, the sign should be greater than (>):

if (salario > 4664.68){
            ↑

Now, you need to adjust the variable scope valIrpf within the function. In the first if, you declared the variable with var and others without the var, that is, the variable will only have scope in the function in the first if, in others will have global scope (can be accessed outside the function).

Before the if's declare the variable with the value 0. If no condition is met, the value of the variable will be 0. Also, instead of using multiple if's nested, use else if to check various conditions.

Your code would look like this:

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];
   var valIrpf = 0; // valores iguais ou abaixo de 1903.98
   
   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;
<h1>Cálculo de Salário</h1>
<p><br>Informe seu salário</p>
<input/>
<button>Calcular</button>

  • Guys, thanks for the help! It’s a silly mistake, but when we’re long looking at the same thing... we don’t even realize.

Browser other questions tagged

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