Discount with JavaScript

0

I've done a JavaScript to calculate discounts for a certain amount, but I do not think it's calculating the percentage correctly. If anyone can help me, I thank you!

  • Paying debit a person earns a 25% discount;
  • Paying off credit without installments earns a 20% discount;
  • Paying in installments the person earns a 5% discount;
  • I think the percentage values I put in js are wrong because you are not calculating correctly. The installment credit gives an absurd value!

    obs: How would you place these discounts only for values above 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)">
        
    asked by anonymous 24.01.2018 / 15:28

    1 answer

    0

    First change the installment payment calculation from:

    valor * 0.5 //50% do valor.
    

    To:

    valor * 0.05 //5% do valor.
    

    Second, avoid using variables like 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 to values above 99.9 simply add the condition:

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

    Follow the example below:

    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><labelfor="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>
        
    29.01.2018 / 12:57