简体   繁体   English

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

[英]How can I add an if else conditional statement to this function?

How can a modify this function so if the user enters less than 2500 cubic ft the price is equal to 0?如何修改此函数,以便如果用户输入小于 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);
}
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);
}
if(cubicft<2500){
price = 0;
}else if (cubicft > 5000) {
  enter code here  price += (cubicft-5000)/1000*22;
} 

This is if you want to check if the user entered less than 2500 and the price is equal to 0. It wasn't clear to me if you wanted to set the price to 0这是如果您要检查用户输入的金额是否小于 2500 且价格是否等于 0。我不清楚您是否要将价格设置为 0

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

if you want to change the price variable you could do something like this :如果您想更改价格变量,您可以执行以下操作:

price = cubicft < 2500 ? 0 : price

ternary operator 三元运算符

Translates to:翻译成:

var price = 42;
if(cubicft < 2500){
    price = 0;
}else{
    price = price
}
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);
}

Here is a working fiddle: https://jsfiddle.net/tspa7tuk/4/这是一个工作小提琴: https : //jsfiddle.net/tspa7tuk/4/

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);

}
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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM