There are two solutions here – one is the algebra that solves the problem you’re actually presenting (because estimation isn’t required here), and the other is the code for doing the estimating if it WERE required.
Algebra:
If we express the markup rate and both tax rates as 1 + increase%
, it makes our math easier. That is, for a markup of 50%, think of that as a multiplier of 1.5; for a tax rate of 10%, think of that as a multiplier of 1.1. Counted that way, your equation is:
unit_price = cost * markup * tax1 * tax2
Because we’re multiplying here and they’re both percentage increases, the order we apply the tax doesn’t actually matter…in fact, the fact that the second tax includes the first tax in its taxable amount makes our math EASIER.
Solving for markup
, that comes out to:
markup = unit_price / (cost * tax1 * tax2)
Code:
Now, the code for doing this sort of estimation – which is interesting, even though this problem doesn’t require it. This problem has some useful traits:
- You can look at a potential answer and not only tell if it’s right, but you can tell if it’s too high or too low
- The values are continuous
- The values are proportional to your input, even if the proportion changes
Given those, you can solve this with a recursive binary search through the space of reasonable values, and perform FAR fewer comparisons than a linear search.
I’d do something like this:
var cost_price = 100;
var unit_price = 130;
var tax_rate_1 = 1.1;
var tax_rate_2 = 1.1;
function estimateMarkup(minMarkup, maxMarkup) {
if (maxMarkup - minMarkup < 0.001) return null;
let markup = (minMarkup + maxMarkup) / 2
let markedupPrice = cost_price * markup * tax_rate_1 * tax_rate_2;
if (Math.round(markedupPrice) == Math.round(unit_price))
return markup;
if (Math.round(markedupPrice) > Math.round(unit_price))
return estimateMarkup(minMarkup,markup);
if (Math.round(markedupPrice) < Math.round(unit_price))
return estimateMarkup(markup,maxMarkup);
}
console.log(estimateMarkup(-100,100))
3
solved More efficient way for calculating margin percent given cost price, unit price, and tax information