Javascript maths problem

Status
Not open for further replies.

slpixe

New Member
Messages
23
Reaction score
0
Points
0
Hey all

I got less than 3 hours to get this sorted >.<!!

I need to be able to calculate this in javascript and its going to require some maths formula I can't seam to land on no matter how many times I play around with it.

Its to work out a parcel price
and this parts based on weight

Less than 500g's is £1.65, Done
Less than 1kg is £3.00, Done

When above 1kg, £1 for each additional 500g!

I have been trying 3 different codes, my last one is the closest, but I been tweaking it loads, and can't get it to land on correct answer.


Code:
/*if (valu3>999)
{price=3;
valu3-1000;
valu3/500;}*/
/*
if (valu3>999)
{price=valu3+499/500}*/

if (valu3>999)
{price=valu3-1000/500}


alert(price);}

Valu3 = weight
price = output

I hope someone's really good at maths that they can work out figures to be able to calculate the price depending on this sort of stuff.

Thanks in advance
 

VPmase

New Member
Messages
914
Reaction score
0
Points
0
Give me a min or so and I'll help.

Edit- Here is my scripts. Works perfectly for every 500g:)

HTML:
<form name="price">
<input type="text" name="wp" id="wp" onkeyup="getPrice();" />G<br />
<input type="text" readonly="readonly" name="price" id="price" />
</form>
<script>
function getPrice(){
	var price;
	var wpp;
	wpp = parseInt(document.price.wp.value);
	if(wpp >= 500){
		var multiplyer = Math.floor(wpp/500);
		var extraprice = 1;
		var origcost = 0;
		var extracost = 0;
		switch(multiplyer){
			case 1:
				//£1.65
				origcost = 1.65;
			break;
			case 2:
				//£3.00;
				origcost = 3.00;
			break;
			default:
				origcost = 3.00;
				extracost = (multiplyer - 2) * extraprice;
			break;
		}
		document.price.price.value = "£" + (origcost + extracost);
	} else {
		document.price.price.value = "";
	}
}
</script>
 
Last edited:

slpixe

New Member
Messages
23
Reaction score
0
Points
0
Oh wow thanks so much!
that's insanely good!

you saved my life XD
+Rep for you =D
 
Last edited:

VPmase

New Member
Messages
914
Reaction score
0
Points
0
You're welcome and thank you for the compliment :)

*Closed*
 
Status
Not open for further replies.
Top