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.
Al4ddin2Asked:
Who is Participating?
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.

hieloCommented:
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);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.