Closed Thread
Results 1 to 4 of 4

Thread: Javascript maths problem

  1. #1
    slpixe is offline x10Hosting Member slpixe is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    23

    Javascript maths problem

    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

  2. #2
    VPmase's Avatar
    VPmase is offline x10 Elder VPmase is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    Dixon, IL, USA
    Posts
    914

    Re: Javascript maths problem

    Give me a min or so and I'll help.

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

    HTML Code:
    <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 by VPmase; 06-15-2008 at 05:56 PM.

  3. #3
    slpixe is offline x10Hosting Member slpixe is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    23

    Re: Javascript maths problem

    Oh wow thanks so much!
    that's insanely good!

    you saved my life XD
    +Rep for you =D
    Last edited by slpixe; 06-15-2008 at 06:23 PM.

  4. #4
    VPmase's Avatar
    VPmase is offline x10 Elder VPmase is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    Dixon, IL, USA
    Posts
    914

    Re: Javascript maths problem

    You're welcome and thank you for the compliment

    *Closed*

Closed Thread

Similar Threads

  1. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  2. JavaScript problem
    By anuj_web in forum Programming Help
    Replies: 25
    Last Post: 04-24-2008, 05:15 AM
  3. DB number problem
    By lionheart8 in forum Free Hosting
    Replies: 5
    Last Post: 04-08-2008, 08:26 AM
  4. Problem with ad code (includes?)
    By Salvatos in forum Free Hosting
    Replies: 10
    Last Post: 12-12-2006, 04:16 PM
  5. Ad code problem!
    By Akkarin in forum Free Hosting
    Replies: 8
    Last Post: 08-29-2005, 10:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers