|
-
May 14th, 2011, 09:02 AM
#1
Thread Starter
Hyperactive Member
[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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|