-1

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 = "";
 }
2
  • 2
    You'll need to append to error.textContent instead of replacing its value. Start with error.textContent = '';, then do error.textContent += 'your error'; whenever you need to show a message. Then you can get rid of the else at the bottom. Commented Oct 28, 2020 at 17:16
  • @RocketHazmat thank you so much! it worked! Commented Oct 28, 2020 at 17:20

1 Answer 1

1

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";
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.