anechkayf
anechkayf

Reputation: 545

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: enter image description here

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

Upvotes: 0

Views: 99

Answers (1)

gen_Eric
gen_Eric

Reputation: 227190

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

Upvotes: 1

Related Questions