Results 1 to 7 of 7

Thread: [RESOLVED] Help with maths in javascript

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Resolved [RESOLVED] Help with maths in javascript

    I have created a instant taxi quote calculator, the function below is the code that is used to calculate the fare from the distance. The problem is i've got my self confused about the maths in particular the < and >. If you do the maths on an normal calculator with the distance as 1.08, the fare should be 2.34

    Code:
    distance = 1.08
    distance - 1 = 0.08
    0.08 * 1.80(PPM) = 0.144
    0.144 + 2.20(FirstMile) = 2.344
    fare = 2.34
    When you run the fare calculator it calculates the fare at: 3.24, when i comment out the if statements and have the FirstMile at 2.20 and PPM at 1.80 it works as it should.

    Thanks in advance, Chris1990

    Code:
    function calcFare() {
    // variables
    	var fareResult;
    	var distance = document.getElementById('txtDistance').value;
     	var firstMile;// = 2.20;// 2.20 for 1st mile
    	var pricepMile;// = 1.80;// 1.80 per mile
    	var TrafficP;// = 1.00;// add 15% for traffic
     //	Select Tarrif
    
    	if (distance > 15); // less than 15 Mile
    	{firstMile = 2.20; // FM £2.20
    	TrafficP = 1.00;// add 00% for traffic
    	pricepMile = 1.80};// PPM £1.80 - standard tariff
    	
    	if (distance > 30); //less than 30 Mile
    	{firstMile = 3.00; // FM £2.50
    	TrafficP = 1.00;// add 15% for traffic
    	pricepMile = 1.40};// PPM £1.40
    
     	if (distance > 50); // less than 50 Mile
    	{firstMile = 3.00; // FM £3.00
    	TrafficP = 1.00;// add 10% for traffic
    	pricepMile = 1.30;}// PPM £1.30
    	
    	if (distance > 100); // less than 100 Mile
    	{firstMile = 3.00; // FM £3.00
    	TrafficP = 1.00;// add 10% for traffic
    	pricepMile = 1.20;}// PPM £1.20
    	
     	if (distance > 175); // less than 250 Mile
    	{firstMile = 3.00; // FM £3.00
    	TrafficP = 1.00;// add 10% for traffic
    	pricepMile = 1.15;}// PPM £1.10
    	
     	if (distance > 300); // less than 500 Mile
    	{firstMile = 3.00; // FM £3.00
    	TrafficP = 1.05;// add 10% for traffic
    	pricepMile = 1.10;}// PPM £1.00
    /*	
     	if (distance > 60); // Over 50 Mile
    	{firstMile = 3.50; // FM £3.50
    	TrafficP = 1.10;// add 10% for traffic
    	pricepMile = 1.20;}// PPM £1.20
    */
      // calculator for Car
      		//exc 1st mile and calc rest of miles by PPM
      	fareResult = (distance - 1) * pricepMile
      		//inc first mile
      	fareResult = fareResult + firstMile;
      	//add percent for traffic
        fareResult = fareResult * TrafficP;	
      // show fare result
    	var roundedNumber = roundNumber(fareResult,2);
    		if (roundedNumber < 1.675) {roundedNumber = "1.70"};	
    	document.getElementById('txtFare').value = roundedNumber;
    	calcType();
    	document.getElementById('btnBookNow').style.visibility='visible'; // show booknow button 
    	}
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  2. #2
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Help with maths in javascript

    Try this:
    Code:
    function calcFare() {
    // variables
    	var fareResult;
    	var distance = document.getElementById('txtDistance').value;
     	var firstMile;// = 2.20;// 2.20 for 1st mile
    	var pricepMile;// = 1.80;// 1.80 per mile
    	var TrafficP;// = 1.00;// add 15&#37; for traffic
     //	Select Tarrif
    
    	if (distance <= 15) // less than 15 Mile
    	{firstMile = 2.20; // FM &#163;2.20
    	TrafficP = 1.00;// add 00% for traffic
    	pricepMile = 1.80};// PPM &#163;1.80 - standard tariff
    	else if (distance <= 30) //less than 30 Mile
    	{firstMile = 3.00; // FM &#163;2.50
    	TrafficP = 1.00;// add 15% for traffic
    	pricepMile = 1.40};// PPM &#163;1.40
    	else if (distance <= 50) // less than 50 Mile
    	{firstMile = 3.00; // FM &#163;3.00
    	TrafficP = 1.00;// add 10% for traffic
    	pricepMile = 1.30;}// PPM &#163;1.30
    	else if (distance <= 100) // less than 100 Mile
    	{firstMile = 3.00; // FM &#163;3.00
    	TrafficP = 1.00;// add 10% for traffic
    	pricepMile = 1.20;}// PPM &#163;1.20
    	else if (distance <= 175) // less than 250 Mile
    	{firstMile = 3.00; // FM &#163;3.00
    	TrafficP = 1.00;// add 10% for traffic
    	pricepMile = 1.15;}// PPM &#163;1.10
    	else if (distance > 300) // less than 500 Mile
    	{firstMile = 3.00; // FM &#163;3.00
    	TrafficP = 1.05;// add 10% for traffic
    	pricepMile = 1.10;}// PPM &#163;1.00
    /*	
     	else if (distance > 60) // Over 50 Mile
    	{firstMile = 3.50; // FM &#163;3.50
    	TrafficP = 1.10;// add 10% for traffic
    	pricepMile = 1.20;}// PPM &#163;1.20
    */
      // calculator for Car
      		//exc 1st mile and calc rest of miles by PPM
      	fareResult = (distance - 1) * pricepMile
      		//inc first mile
      	fareResult += firstMile;
      	//add percent for traffic
        fareResult = fareResult * TrafficP;	
      // show fare result
    	var roundedNumber = roundNumber(fareResult,2);
    	if (roundedNumber < 1.675) {roundedNumber = "1.70";}
    	document.getElementById('txtFare').value = roundedNumber;
    	calcType();
    	document.getElementById('btnBookNow').style.visibility='visible'; // show booknow button 
    }
    It'd help if you posted what should happen in English and then we can try to write it for you

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: Help with maths in javascript

    thanks for your help, sorry i thought i made it clear that the fare calculator was for some reason giving an incorrect fare of &#163;3.24 based on a distance of 1.08 miles. If you do the maths on a normal calculator, it will give the correct fare of &#163;2.34.

    I tried using the code in your reply, but it stops the google map plugin on the page from loading. I have a working version of the fare calculator here, this version is based on the standard fare only (2.20 - first mile, 1.80 per mile). I am trying to implement a lower fare based on a larger distance.

    thanks chris1990
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: Help with maths in javascript

    As you can see i've reversed the code, using the fare calculator with a distance of 1.08 miles now correctly displays a price of &#163;2.34. However when using a larger distance such as 195.14 miles its displays a price of &#163;351.65, when it should be &#163;226.26

    thanks chris1990

    Code:
     	if (distance <= 300); // less than 300 Mile
    	{firstMile = 3.00; // FM &#163;3.00
    	TrafficP = 1.00;// add 10&#37; for traffic
    	pricepMile = 1.10;}// PPM &#163;1.00
    
     	if (distance <= 175); // less than 150 Mile
    	{firstMile = 3.00; // FM &#163;3.00
    	TrafficP = 1.00;// add 10% for traffic
    	pricepMile = 1.15;}// PPM &#163;1.10
    	
    	if (distance <= 100); // less than 100 Mile
    	{firstMile = 3.00; // FM &#163;3.00
    	TrafficP = 1.00;// add 10% for traffic
    	pricepMile = 1.20;}// PPM &#163;1.20
    	
    	if (distance <= 50); // less than 50 Mile
    	{firstMile = 3.00; // FM &#163;3.00
    	TrafficP = 1.00;// add 10% for traffic
    	pricepMile = 1.30;}// PPM &#163;1.30
    	
    	if (distance <= 30); //less than 30 Mile
    	{firstMile = 3.00; // FM &#163;2.50
    	TrafficP = 1.00;// add 15% for traffic
    	pricepMile = 1.40};// PPM &#163;1.40
    
    	if (distance <= 15); // less than 15 Mile
    	{firstMile = 2.20; // FM &#163;2.20
    	TrafficP = 1.00;// add 00% for traffic
    	pricepMile = 1.80};// PPM &#163;1.80 - standard tariff
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Help with maths in javascript

    Your sequence is backwards. If you are checking less-than conditions you need to start with the lowest number first, otherwise you end up executing every block.

    I would try a different approach:
    Code:
    function calcFare(d)
    {
    	var tariffs = [
    		{d: 15, fm:2.2, tm:1.00, ppm:1.80},
    		{d: 30, fm:3.0, tm:1.15, ppm:1.40},
    		{d: 50, fm:3.0, tm:1.10, ppm:1.30},
    		{d:100, fm:3.0, tm:1.10, ppm:1.20},
    		{d:175, fm:3.0, tm:1.10, ppm:1.15},
    		{d:300, fm:3.0, tm:1.10, ppm:1.10}
    	];
    
    	for (var i in tariffs)
    	{
    		var T = tariffs[i];
    		if (d <= T.d)
    			return Math.max(2.2, (T.fm + (d - 1) * T.ppm) * T.tm);
    	}
    
    	return 2.2;
    }
    I assume that 2.20 is the minimum fare.
    Also, you will need to change the actual tariff numbers. Mine are guesses based on your previous code snippets and comments, all of which seem to be in disagreement.

    Also also... you don't have a condition for a distance greater than 300 miles.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: Help with maths in javascript

    Thanks for your help penagate, i used a modified version of your code and got it working as i wanted.

    The note you made about my sequence being backwards doesn't seem to be the case, using your code it ran the 300 line last, meaning the ppm is always 1.10 for anything under 300 miles. Where as backwards it runs the 15 line last which makes it work fine.

    thanks, chris1990
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Help with maths in javascript

    Quote Originally Posted by chris1990 View Post
    The note you made about my sequence being backwards doesn't seem to be the case, using your code it ran the 300 line last, meaning the ppm is always 1.10 for anything under 300 miles. Where as backwards it runs the 15 line last which makes it work fine.
    I think one of us is still confused...

    Let's step through my code for a distance (d) of 20mi.
    For the first tariff checked, T.d is 15 and so d<=T.d is not true.
    For the next tariff checked, T.d is 30 and so d<=T.d is true and the function exits.

    I don't see how you how got "ppm is always 1.10 for anything under 300 miles" from this.

    I suppose as long as you've got it working, it doesn't matter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width