mercredi 12 août 2020

if statement problem, could you please, explain?

can someone please explain me what is the problem with my If statement ? My code is

if(row < coloredRow || column < coloredColumn) {
  return alert('Error')
} else { 
  //return code
}

when I write 5 5 2 2 to the inputs it is ok and work but when I put 10 10 2 2 it triggers the Error alert. Here is the live version in codepen(https://codepen.io/ruslancik/pen/mdPeaYw?editors=1111) just try 5 5 2 2 and later 10 10 2 2

function createTable() {
  var table = document.getElementById("myTable");
  var row = document.getElementById("row").value;
  var column = document.getElementById("column").value;
  var coloredRow = document.getElementById("rowColor").value;
  var coloredColumn = document.getElementById("columnColor").value;
  if(row < coloredRow || column < coloredColumn) {
    return alert('Error')
  } else {
    for (var i = 0; i < row; i++) {
      var x = table.insertRow(i);
      for (var j = 0; j < column; j++) {
        var y = x.insertCell(j);
        y.innerHTML = "Row-" + i + " Column-" + j;
      }
    }
    for (var z = 0; z < row; z++) {
      table.rows[coloredRow - 1].style.backgroundColor = "red";
      table.rows[z].cells[coloredColumn - 1].style.backgroundColor = "blue";
      table.rows[coloredRow - 1].cells[coloredColumn - 1].style.backgroundColor = "green";
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire