JavaScript if else 条件混淆

时间:2021-03-09 14:57:32

标签: javascript

我有以下代码

if (gridObj.INVOICEORDERNUMBER) {
  if (!cancelledStatus.length && !withDrawn.length) {
    this.gridCmp.editValidation = true;
    this.errorMessage = ErrorMessage.OpenOrderMsg;
  } else {
    this.relatedLicenses = false;
    this.hasDRLPLicense = false;
    this.deletePopupMsg = ErrorMessage.DeletePopupMsg;
    this.showDeletePopup = true;
  }
} else if (this.hasDRLPLicense) {
  this.gridCmp.editValidation = true;
  this.errorMessage2 = ErrorMessage.DRLPLicenseDeleteMsg;
} else {
  this.relatedLicenses = false;
  this.hasDRLPLicense = false;
  this.deletePopupMsg = ErrorMessage.DeletePopupMsg;
  this.showDeletePopup = true;
}

这里我需要检查两个 if 条件是否都满足。使用当前的方法,我只能检查任何人 if 条件,因为它是一个 if-else, if 语句。

我需要在这里实现的是

  1. 如果满足第一个 if 条件 -> show errorMessage
  2. 如果第二个 if 条件(else if)满足 -> show errorMessage2
  3. 如果两者都满意 -> show both errorMessage and errorMessage2
  4. 如果都不满意 -> execute else

这可能是一个愚蠢的问题,但我的大脑今天不再工作了。请帮忙。谢谢

2 个答案:

答案 0 :(得分:3)

根据你解释的逻辑,这就是你要找的真值表:

cond1 | cond2 | errorMessage | errorMessage2 | execute 
------+-------+--------------+---------------+--------
false | false |  NO          |  NO           |  YES
false | true  |  NO          |  YES          |  NO
true  | false |  YES         |  NO           |  NO
true  | true  |  YES         |  YES          |  NO

这是完成它的代码:

if (cond1 || cond2) {
  if (cond1) { 
    // show errorMessage
  }

  if (cond2) { 
    // show errorMessage2
  }
} else {
  // execute ...
}

答案 1 :(得分:0)

试试这个代码。

Term
相关问题