如何在此函数中添加if else条件语句?

时间:2016-10-12 19:59:50

标签: javascript if-statement

如果用户输入小于2500立方英尺的价格等于0,如何修改此功能?

function updateCombustibleMaterialFireFee(cubicft) {
    var price = 42;
    if (cubicft > 5000) {
        price += (cubicft-5000)/1000*22;
    } 

    // Change the parameters to the correct fee code, fee schedule, etc.
    // updateFee(fcode, fsched, fperiod, fqty, finvoice, pDuplicate, pFeeSeq)
    updateFee("FPERMIT47","FIRE","FINAL",price,"N","N");

    logDebug("$" + price);
}

6 个答案:

答案 0 :(得分:2)

function updateCombustibleMaterialFireFee(cubicft) {
    var price = 42;
    if (cubicft < 2500) {
        price = 0;
    }
    else if (cubicft > 5000) {
        price += (cubicft - 5000) / 1000 * 22;
    }

    // Change the parameters to the correct fee code, fee schedule, etc.
    // updateFee(fcode, fsched, fperiod, fqty, finvoice, pDuplicate, pFeeSeq)
    updateFee("FPERMIT47", "FIRE", "FINAL", price, "N", "N");

    logDebug("$" + price);
}

答案 1 :(得分:1)

if(cubicft<2500){
price = 0;
}else if (cubicft > 5000) {
  enter code here  price += (cubicft-5000)/1000*22;
} 

答案 2 :(得分:0)

如果您想检查用户是否输入的价格低于2500且价格等于0.我想知道您是否要将价格设置为0

if(cubicft < 2500 && price == 0){
    //do something
}

如果您想更改价格变量,您可以执行以下操作:

price = cubicft < 2500 ? 0 : price

ternary operator

转换为:

var price = 42;
if(cubicft < 2500){
    price = 0;
}else{
    price = price
}

答案 3 :(得分:0)

function updateCombustibleMaterialFireFee(cubicft) 
{
    var price;
    if(cubicft > 5000)
        price = 42 + (cubicft-5000)/1000*22;
    else if(cubicft < 2500)
        price = 0;
    else
        price = 42;
    // Change the parameters to the correct fee code, fee schedule, etc.
    // updateFee(fcode, fsched, fperiod, fqty, finvoice, pDuplicate, pFeeSeq)
    updateFee("FPERMIT47","FIRE","FINAL",price,"N","N");
    logDebug("$" + price);
}

这是一个工作小提琴:https://jsfiddle.net/tspa7tuk/4/

答案 4 :(得分:0)

function updateCombustibleMaterialFireFee(cubicft){
var price;
    switch (true) {
      case cubicft < 2500: 
        price=0;
        break;

      case cubicft > 5000:
        price = 42 + (cubicft-5000)/1000*22; 
        break;

      default:
        price = 42;
        break;
   }

  // Change the parameters to the correct fee code, fee schedule, etc.
  // updateFee(fcode, fsched, fperiod, fqty, finvoice, pDuplicate, pFeeSeq)
  updateFee("FPERMIT47","FIRE","FINAL",price,"N","N");

  logDebug("$" + price);

}

答案 5 :(得分:0)

function updateCombustibleMaterialFireFee(cubicft) {
var price;

if (cubicft < 2500) {
price = 0;
}
else if (cubicft > 5000) {
price = 42 + (cubicft-5000)/1000*22;
}
else {
price = 42;
}

updateFee("FPERMIT47", "FIRE", "FINAL", price, "N", "N");
logDebug("$" + price);
}