# javascript price lookup calculation

I am creating a javascript function (using only pure javascript)

That will read in a dynamic quantity as a variable and calculate the price depending.

The pricing will be tiered. The tiers are between 2 - 20, 21 - 50 and >50.
There is always a base price of 1.99 added, then the additional costs are determined by the tiers.
2-20 = 0.99
21 - 50 = 0.50
>50 = 0.25
qty            price
2            1.99   (base price)
4            1.99 + (2 * 0.99)
base price + additional qty charged at lower price
6            1.99 + (4 * 0.99)
...
20            1.99 + (18 * 0.99)
22            1.99 + (18 * 0.99) + (2 * 0.50)
...
50            1.99 + (18 * 0.99) + (30 * 0.50)
...
78            1.99 + (18 * 0.99) + (30 * 0.50) + (28 * 0.25)

This is how the price gets worked out and I just need to create a lookup function that reads in the qty and returns a price based on the above.
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
``````function getPrice(qty)
{
var temp = qty;
var price = 1.99;
if( temp > 50)
{
price+=(temp-50)*0.25;
temp-=(temp-50);
}

if( temp > 20 )
{
price+=(temp-20)*0.50;
temp-=(temp-20);
}

if( temp > 2 )
{
price+=(temp-2)*0.99;
temp-=(temp-2);
}

return price.toFixed(2);
}
``````

Experts Exchange Solution brought to you by