-1

I have below code

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

Here I need to check whether both if conditions satisfy or not. With the current approach, I can only check anyone if condition, as it is an if-else, if statement.

What I need to achieve here is

  1. If first if condition is satisfied -> show errorMessage
  2. If second if condition (else if) satisfied -> show errorMessage2
  3. If both satisfied -> show both errorMessage and errorMessage2
  4. If none satisfied -> execute else

This might be a stupid question, but my brain is not working any more today. Please help. Thanks

2
  • what is the other condition? i see three ifs. Commented Mar 9, 2021 at 15:01
  • Move 3 up to 1, and move original 1 and 2 down. Commented Mar 9, 2021 at 15:02

2 Answers 2

2

According to the logic you explained, this is the truth table you are looking for:

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

This is the code which will accomplish it:

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

  if (cond2) { 
    // show errorMessage2
  }
} else {
  // execute ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

@ManasKhandelwal maybe you should be a bit more conservative with your -1s. He's correct. This is precisely what OP describes in his 4 points.
@Dropout My bad 😓.
Thanks @Dropout. Lately it became a fashion to give -1s too fast :-(
Thanks Man, you saved my day.. now I can have some sleep :-)
0

Try this code.

if (gridObj.INVOICEORDERNUMBER || this.hasDRLPLicense) {
  this.gridCmp.editValidation = true;

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

1 Comment

Thanks Tatul for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.