5

I am trying to calculate in javascript. Some parts of the calculation are not working.

I want to calculate the total price with discount. For example: When price = 10.00 And discount = 50 (%)

I need to calculate: 10.00 * 0,50

So I need something like this:

 var totalValue = numVal1 / 0, numVal2

When I do this, the script is not working. How can I fix this?

This is my full code:

function getPrice() {         
 var numVal1 = Number(document.getElementById("price").value);
 var numVal2 = Number(document.getElementById("discount").value);

 var totalValue = numVal1 / numVal2
 document.getElementById("total").value = totalValue.toFixed(2);
}
John
  • 904
  • 8
  • 22
  • 56

7 Answers7

8

So you have a price ( 10 ) and a discount ( 50 )

FinalPrice = price * ( 100-discount / 100 ) = 10 * ( 100-50 / 100 ) = 10 * 0.5 = 10 * 50%

So

function getPrice() {         
 var numVal1 = Number(document.getElementById("price").value);
 var numVal2 = Number(document.getElementById("discount").value);

 var totalValue = numVal1 * ( (100-numVal2) / 100 )
 document.getElementById("total").value = totalValue.toFixed(2);
}
Jérôme
  • 81
  • 3
4

I think that you are not calculating adecually the value, it should be:

var totalValue = numVal1 *(numVal2/100);
1

To make it a percent you'd divided the discount by 100 and multiplied it by the price.

For example I bought some shoes, £50 is the normal price but it was a closing down sale so 75% off. so we need to work out 75% of 50 and then take that off.

50 - (50 * 75 / 100) or 50 - (50 * 0.75)

(The shoes were only £12.50... bargain!)

I would go for something like:

getPrice = function() {
  var numVal1 = Number(document.getElementById("price").value);
  var numVal2 = Number(document.getElementById("discount").value) / 100;

  var totalValue = numVal1 - (numVal1 * numVal2)
  document.getElementById("total").value = totalValue.toFixed(2);
}
<input id="price">
<br>
<input id="discount">%
<br>
<button onclick="getPrice()">
  Get total
</button>
<br>
<input readonly id="total">
Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
1
var result =  (numVal1 -numVal2 )/numVal1;
var result2 = result*100;
B001ᛦ
  • 2,036
  • 6
  • 23
  • 31
Tayyab
  • 11
  • 1
0

You can't expect JavaScript to understand that 0, numVal2 is supposed to be inferred to a floating number. The comma has its own meaning in JavaScript. Instead, you should treat numVal2 as a percentage (which it is) and do :

var totalValue = numVal1 / (numVal2 / 100);
LoremIpsum
  • 4,328
  • 1
  • 15
  • 17
0

Try this one.

function calculateDiscount(price,discount) {
    return (price - (price * (discount/100))).toFixed(2);
}


document.getElementById("submit_btn").onclick = function(){
  var price = Number(document.getElementById("price_input").value);
  var discount =    Number(document.getElementById("discount_input").value);
  var calculated = calculateDiscount(price,discount);
  document.getElementById("price_tag").innerHTML = calculated;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <strong>Enter  Price</strong><br>
    <input type="text" placeholder="Price" id="price_input"><br><br>
     <strong>Enter Discount %</strong><br>
    <input type="text" placeholder="Discount %" id="discount_input"><br><br>
    <button type="button" id="submit_btn">Calculate</button>
    
    <h1 id="h1_tag">Final:. <small id="price_tag">0.00</small></h1>
</body>
</html>
Vikas Kandari
  • 1,612
  • 18
  • 23
0

This programme demonstrates how to calculate discount.

const originalPrice = parseInt(1000);
const discount = parseInt(30);
const afterDiscount = originalPrice - (originalPrice * discount / 100);
console.log("Final Price is: " + afterDiscount);
BK Uddin
  • 1
  • 1