1

i would like to set a background color for each number that is a multiple of 3 and 2 how can i do this? i'm not very good at this javascript stuff

HTML:

<div id="mainDiv">
    <input type="text" id="myNo" />
    <input type="button" id="gridSize" value="Show It" />
</div>

Javascript:

var button = document.getElementById('gridSize');
 button.onclick = function(e){
    result  = document.getElementById('result');
    num = parseInt(document.getElementById('myNo').value);
    alert(num);
    if ( isNaN(num) ){
        alert("No Numeric Value!");
    }
    else{
        result.innerHTML = num;
    }
    var str = "<table border='2'>";
    for (row = 0; row < num; row++){
    str += "<tr>";
        for (col = 0; col < num; col++){
            str += "<td>";
            var randNum = Math.floor(Math.random() * 100) + 1;
            if (randNum % 2){
                console.log(randNum + "Red");
            }
            str += randNum;
            str += "</td>";
        }
        str += "</tr>";
     }
     str = str + "</table>";
     result.innerHTML = str;
}
2
  • by a multiple of 3 and 2 do you mean 6? if not, what happens when it's a multiple of both? Commented Apr 2, 2016 at 20:54
  • viiiyears post is what i was looking for Commented Apr 3, 2016 at 18:27

3 Answers 3

1

If you put your td creations into a if else statement you can do it pretty simply like this:

HTML

<div id="mainDiv">
  <input type="text" id="myNo" />
  <input type="button" id="gridSize" value="Show It" />
</div>

<div id="result"></div>

CSS

.two {
  background: red;
}

.three {
  background: blue;
}

JS

var button = document.getElementById('gridSize');
button.onclick = function(e) {
  result = document.getElementById('result');
  num = parseInt(document.getElementById('myNo').value);
  alert(num);
  if (isNaN(num)) {
    alert("No Numeric Value!");
  } else {
    result.innerHTML = num;
  }
  var str = "<table border='2'>";
  for (row = 0; row < num; row++) {
    str += "<tr>";
    for (col = 0; col < num; col++) {
      var randNum = Math.floor(Math.random() * 100) + 1;
      if (randNum % 2 === 0) {
        str += '<td class="two">';
      } else if (randNum % 3 === 0) {
        str += '<td class="three">';
      } else {
        str += "<td>";
      }
      str += randNum + "</td>";
    }
    str += "</tr>";
  }
  str = str + "</table>";
  result.innerHTML = str;
}

jsfiddle here

Sign up to request clarification or add additional context in comments.

Comments

1

This is how you check, if its dividable by 2 and 3:

if(randNum %2===0 && randNum %3===0){
  // do some
}

Comments

0

Here is the way you can do that(similar to your code)

var resultTable  = document.createElement('table');
var tr = resultTable.insertRow();

tbl.style.border = '1px solid black';

for (row = 0; row < num; row++){
    var tr = resultTable.insertRow();
    for (col = 0; col < num; col++){
        var td = tr.insertCell();
        var randNum = Math.floor(Math.random() * 100) + 1;
        //for example if you want numbers countable with 2 to be red
        if ((randNum % 2) === 0){
            td.style.backgroundColor = "red";                 
        }                        
        td.appendChild(document.createTextNode(randNum.toString()));
    }
 }
 result.appendChild(resultTable);

3 Comments

Where do you check if its dividable by 3?
I'm not sure about what he really needs. I checked dividable by 2 as an example he can modify as he desires.
look at viiiyears post at the bottom its exactly what i was looking for

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.