Javascript Discount

Asked

Viewed 3,065 times

1

I made a Javascript to calculate discounts of a certain value, but I think it is not calculating the percentage correctly. If anyone can help me, I’d appreciate it!

  1. Paying in debt person gets 25% discount;
  2. Paying on credit without installments wins 20% discount;
  3. Paying installments person earns 5% discount;

I believe that the percentage values I put in js are wrong because it is not calculating in the right way. The credit installment gives an absurd value!

BS: how would you put these discounts only for amounts over 99.99?

function calcular(){
	var x = document.getElementById("valor");
	var pagar = document.getElementById("pagar");
	var d = document.getElementById("debito");
	var parc = document.getElementById("parcelado");
	var semp = document.getElementById("semparcelas");
		var p = Number(x.value);
		var y = p;
		var s=0;
			if(parc.checked){
				y = (y*0.5);
				s= p-y;
			}
			if(semp.checked){
				y = (y*0.20);
				s= p-y;
			}
			if(d.checked){
				y = (y*0.25);
				s= p-y;
			}
	pagar.value= "Desconto: "+y+". Pagar: "+s;
}
<label for="valor"> Digite o valor da compra: </label>
<br>
<input type="text" id="valor" name="valor" required onfocus="fn(this)" onblur="fs(this)">
<br>
<br> Qual sua forma de pagamento?
<br>
<input type="radio" name="pagamento" id="debito" value="debito">
<label for="debito"> Débito </label>
<br>
<input type="radio" name="pagamento" id="parcelado" value="parcelado">
<label for="parcelado"> Crédito parcelado </label>
<br>
<input type="radio" name="pagamento" id="semparcelas" value="semparcelas">
<label for="semparcelas"> Crédito sem parcelas</label>
<br></br>
Desconto e valor a pagar:
<br>
<input type="text" id="pagar" name="pagar" disabled>
<input id="botao1" type="button" value="Calcular" onclick="calcular()" onmouseover="botao(this)" onmouseout="botao(this)">

  • 1

    Here the figures are correct. Why do you think it’s not working? Can you edit the question and put what is causing the wrong values?

  • For some reason, the installment credit is giving a higher value in discount compared to others, and it should be the other way around. Maybe I used the values backwards, you could help me?

  • You are giving 50% off. The exact line is y = (y*0.5);. You may have to revise your code, especially in the "rules of 3" of your programming. Anyway your question is marked as pending because it is not clear enough in the question. How exactly should the discount be?

  • I edited the question. It should be 25%, 20% and 5%

  • *0.5 is 50% ... 5% is *0.05

  • Exactly as Leandro said change this passage: y = (y*0.05);

Show 1 more comment

1 answer

3

First change the calculation of the instalment payment of:

valor * 0.5 //50% do valor.

To:

valor * 0.05 //5% do valor.

Second, avoid the use of variables such as x, y, z this makes it difficult to read and maintain the code, try to use names that suggest what the variable contains.

Finally to apply the discount only for values above 99.9 just add the condition:

if(valor>99.9){
   //lógica...
}

Below is an example:

function calcular(){
	var valor             = document.getElementById("valor").value;
	var resultado         = document.getElementById("pagar");
	var debito            = document.getElementById("debito");
	var creditoParcelado  = document.getElementById("parcelado");
	var credito           = document.getElementById("semparcelas");
  var desconto          = 0;
  
  if(valor>99.9){
    if(creditoParcelado.checked){
      desconto = valor*0.05; //5%
      valor = valor*0.95;

    }

    if(credito.checked){
      desconto = valor*0.20;
      valor = valor*0.80;
    }

    if(debito.checked){
      desconto = valor*0.25;
      valor = valor*0.75
    }
  }
  
	resultado.value = "Desconto: "+desconto+". Pagar: "+valor;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <label for="valor"> Digite o valor da compra: </label> <br>
  <input type="text" id="valor" name="valor" required>
</div>
<div>
  <p>Qual sua forma de pagamento?</p>
  <input type="radio" name ="pagamento" id="debito" value="debito"> 
  <label for="debito"> Débito </label>
  <br>
  <input type="radio" name="pagamento" id="parcelado" value="parcelado"> 
  <label for="parcelado"> Crédito parcelado </label>
  <br>
  <input type="radio" name="pagamento" id="semparcelas" value="semparcelas"> 
  <label for="semparcelas"> Crédito sem parcelas</label>
</div>
<div>
  <p>Desconto e valor a pagar: </p>
  <input type="text" id="pagar" name="pagar">
  <input id="botao1" type="button" value="Calcular" onclick="calcular()">
</div>

Browser other questions tagged

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