Price Calculation based on the distance covered

一曲冷凌霜 提交于 2019-12-23 05:20:03

问题


I need to calculate price this is what i've got

case1: if a car travels between 1-15 miles then price for first mile is £3 and then for every mile after it price will be £1.60

so for this case i'm calculating like this

miles= 1-15
 rate=1.60;
  emile=miles-1;
  totalprice=miles*passenger*1.60+3;

case2 if a car travels between 15-30 miles then price will be £ 1.20/mile+previous formula

so for this case i'm calculating like this

   rate=1.20;
    rate2=1.60;
    cmile=(miles-14)*rate;
    fmile=(14*rate2+3);
    totalprice=cmile+fmile;

case3:if a car travels between 30-50 miles then price will be £ 1.10/mile+both previous formula

now i'm stuck here how do i calculate for 3rd case


回答1:


Try,

var kms = 10;

var price_1 = (kms > 0) ? 3 : 0; kms =  (kms > 0)? kms - 1 :  0;
var price_2 = (kms - 14) > 0 ? (14 * 1.60) : (kms * 1.60); kms = (kms-14)>0 ? kms - 14 : 0;
var price_3 = (kms - 15) > 0 ? (15 * 1.40) : (kms * 1.40); kms = (kms-15)>0 ? kms - 15 : 0;
var price_4 = (kms > 0) ? (kms * 1.20) : 0;


console.log('Total fare would be :' + (price_1 + price_2 + price_3 + price_4));

DEMO




回答2:


CMIIW

for case 3, since the car passed 29miles, we can just multiply case1 & 2 with their max miles

rate1 = 1.60; rate2 = 1.20;
fmile = 14*rate1; //1 to 14
smile = 15*rate2; //15 to 29

and do the same for third formula, with additional condition of 50 miles limit

rate3 = 1.10;
if (miles%50 < 1){
   tmile = (miles - 29)*rate3;
}else{
   tmile = 20*rate3;
}

sum it all and add the fixed price

 totalprice = 3 + fmile + smile + tmile;



回答3:


var miles = 47;
switch (miles) {
       if( miles > 0 && miles < 15)
        {
            miles = miles*1;
        }
    if(miles > 15 && miles < 30)
        {
            miles = ((miles-15)*1.5)+(15*1);
        }
        if( miles > 30 && miles < 50)
        {
            miles = ((miles-30)*2)+(15*1.5)+(15*1);
        }
        if(miles > 50 && miles < 100)
        {
            miles = ((miles-50)*2.5)+(20*2)+(15*1.5)+(15*1);
        }
}
alert(miles);


来源:https://stackoverflow.com/questions/24179939/price-calculation-based-on-the-distance-covered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!