Calculate percentage by value or value by percentage

https://stackoverflow.com/questions/35759408/calculate-percentage-by-value-or-value-by-percentage

by Good Muyis

HTML

Regular Price : <input type="text" name="price" value="33000"><br>
Discount Percentage : <input type="text" name="percentage" onkeyup="calculatePrice()" value="33"><br>
Discounted Price : <input type="text" name="discount" onkeyup="calculatePerc()">

JavaScript

function calculatePrice() {
    var percentage = $('input[name=\'percentage\']').val(),
        price = $('input[name=\'price\']').val(),
        calcPrice = price - ( (price/100) * percentage ),
        discountPrice = calcPrice.toFixed(2);
    $('input[name=\'discount\']').val(discountPrice);
}
function calculatePerc() {
    var discountPrice = $('input[name=\'discount]\']').val(),
        price = $('input[name=\'price\']').val(),
        calcPerc = (price/100) * (price-discountPrice),
        discountPerc = calcPerc.toFixed();
    $('input[name=\'percentage\']').val(discountPerc);
}