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!
- Paying in debt person gets 25% discount;
- Paying on credit without installments wins 20% discount;
- 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)">
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?
– bio
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?
– Ana
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?– bio
I edited the question. It should be 25%, 20% and 5%
– Ana
*0.5
is50%
...5%
is*0.05
– Leandro Angelo
Exactly as Leandro said change this passage:
y = (y*0.05);
– Caique Romero