简体   繁体   中英

JavaScript if/else conditions

I'm trying to print the error message if the minimum value > maximum value for both horizontal and vertical directions for my multiplication table.

For example: 在此处输入图片说明

For some reason, my code only prints the error message if the Row values have to be swapped, it doesn't print the message for Column values. Also, I need a case if they both have to be swapped , but when I add this condition nothing works(I left it commented out).

script.js:

 if(minCol>maxCol)
 {
   let temp = maxCol;
   maxCol = minCol;
   minCol = temp;
   error.textContent = "Minimum Column Value has been swapped with Maximum Column Value.";
   error.style.color = "red";
 }
 if(minRow>maxRow)
 {
   let temp = maxRow;
   maxRow = minRow;
   minRow = temp;
   error.textContent = "Minimum Row Value has been swapped with Maximum Row Value.";
   error.style.color = "red";
 }
 /*
 if(minCol>maxCol && minRow>maxRow)
 {
   error.textContent = "Minimum Column and Row Value has been swapped with Maximum Column and Row Value.";
   error.style.color = "red";
 }
 */
 else 
 {
   error.textContent = "";
 }

You are replacing error.textContent every time you hit one of your if conditions, meaning only error message can be set at a time. Also, your else is deleting the message if minRow > maxRow is false, ignoring the minCol > maxCol condition.

Try setting error.textContent = ''; at the beginning and then appending to the error message.

error.textContent = "";

if(minCol>maxCol)
{
   let temp = maxCol;
   maxCol = minCol;
   minCol = temp;
   error.textContent += "Minimum Column Value has been swapped with Maximum Column Value.";
   error.style.color = "red";
}

if(minRow>maxRow)
{
   let temp = maxRow;
   maxRow = minRow;
   minRow = temp;
   error.textContent += "Minimum Row Value has been swapped with Maximum Row Value.";
   error.style.color = "red";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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