I need to do some calculation for a mileage price in jQuery.
Say for example distance = 10
On a tariff such as:
----------------------------
| Mileage | Rate |
----------------------------
| 1 | 1.5 |
----------------------------
| 5 | 3 |
----------------------------
| 999 | 1.2 |
----------------------------
Which means it would be £1.50 for the first mile, £3.00 for the 2nd to 5th and then £1.20 for every additional mile.
This would make the 10 mile journey = £19.50
I don't really know where to start so any help would be much appreciated.
All I can think of is:-
var distance = 10
// create a key value pair (although this isn't correct)
var tariff = {mileage: 1, rate: 1.5, mileage: 5, rate: 3, mileage: 999, rate: 1.2};
and then looping the tariff:-
for (var i = 0; i < distance; i++) {
//multiply mileage by rate
}
//get sum of values from loop