JSFiddle - React, Tailwind, and code Playground
HTML
width <input id="width" value="2"/><br/>
height <input id="height" value="2"/><br/>
<br/>
<br/>
<div id="priceList"><li></li></div>
JavaScript
var availableQuantities = [50,100,200,300,500,1000,2000,3000,5000,10000]
var min_square_inch_price = 0.018
var area_coefficient = 0.15
var area_exponent = -0.18
var max_fixed_charge = 40
var min_fixed_charge = 2.0
var quantity_exponent = -0.3
var quantity_coefficient = 0.32
displayContainer()
$('input').keyup(function() {
displayContainer()
});
function displayContainer() {
var sqInch = $("#width").val() * $("#height").val(),
container = $("#priceList").empty()
$.each(availableQuantities, function( index, value ) {
var price = calculatePrice(sqInch, value),
savings = calculateSaving(sqInch, value);
container.append('<div>quantity ' + value + ' price ' + price + ' savings ' + savings + '</div>');
});
}
function calculatePrice(sqInch, quantity) {
var c = sqInch * quantity
d = Math.max(min_square_inch_price, area_coefficient * Math.pow(c, area_exponent)),
e = quantity_coefficient * Math.pow(quantity, quantity_exponent);
return Math.round(d * c + e * quantity + fixedCharge(quantity));
}
function fixedCharge(quantity) {
var b = Math.log(max_fixed_charge / min_fixed_charge) / Math.log(50),
c = min_fixed_charge * Math.pow(quantity, b);
return Math.round(Math.min(max_fixed_charge, c))
}
function calculateSaving(sqInch, quantity) {
var c = availableQuantities[0],
d = calculatePrice(sqInch, c) / c,
e = calculatePrice(sqInch, quantity) / quantity;
return Math.round(100 * ((d - e) / d))
}