Как я могу добавить условный оператор if else к этой функции?

#javascript #if-statement

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

Комментарии:

1. Добавить? } else if () {} ??

2. else if (cubicft <2500) {price = 0}

Ответ №1:

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

Ответ №2:

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

Ответ №3:

 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:

Это если вы хотите проверить, ввел ли пользователь менее 2500, а цена равна 0. Мне было непонятно, хотите ли вы установить цену равной 0

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

если вы хотите изменить переменную price, вы можете сделать что-то вроде этого :

price = cubicft < 2500 ? 0 : price

троичный оператор

Переводится как:

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

Ответ №5:

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

}
  

Ответ №6:

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