Reputation: 449
First I have a form, with 2 fileds board
field and myNumb
, the board is the number that controls the table size. myNumb
is the number of cells that should be selected randomly.
I am trying to select random table cells with javascript. I have a integer variable myNumb
where I will give how many random cells should be selected. In the selected cells I want to put 1 and the rest of them 0. I have built the structure of the table, but don't have a idea how to select random cells. Any ideas would be much appreciated.
function tableMatrix () {
//taking values from the form
var board = document.forms["myForm"]["board"].value;;
var myNumb = document.forms["myForm"]["myNumb"].value;
var zero = 0;
var one = 1;
//finding myContainer
var myContainer = document.getElementById('myContainer');
myContainer.innerHTML = '';
//creating element <table>
var table = document.createElement('table');
table.style.width = '100%';
table.setAttribute('border', '1');
//creating element <tbody>
var tbody = document.createElement('tbody');
//cells creation
for (var i = 0; i < board; i++) {
//creating <tr> element
var tr = document.createElement('tr');
for (var j = 0; j < board; j++) {
//creating <td> element
var td = document.createElement('td');
//put <td> after <tr> element
tr.appendChild(td);
td.setAttribute('class', 'tblCells');
var x = 0;
if (x <= myNumb) {
var randomCounter = Math.floor(Math.random() * 9);
x += randomCounter;
td.innerHTML = '<span>'+ one +'</span>';
} else {
td.innerHTML = '<span>'+ zero +'</span>';
}
}
//put <tr> after <tbody> element
tbody.appendChild(tr);
}
//put <tbody> after <table> element
table.appendChild(tbody);
//put <table> after <div> element
myContainer.appendChild(table);
}
I have created also a JSFiddle here if it helps
Upvotes: 2
Views: 3945
Reputation: 121
Here you go.
function selectRandomCells(board, myNumb, table) {
var totalCells = board * board;
var selectedCell, cellIndex = null;
for(var i=1; i<=myNumb; i++) {
cellIndex = Math.floor((Math.random() * totalCells) + 1);
selectedCell = table.rows[Math.floor(cellIndex/board)].cells[cellIndex%board];
selectedCell.innerHTML = "<span>0</span>";
if(i == 1) {
selectedCell.innerHTML = "<span>1</span>";
}
}
}
Demo updated > https://jsfiddle.net/souravb/9ot5ryf3/5/
p.s: I have not included the logic of repeating random numbers. For that you can simply store the random numbers in an array and check every time a new number is generated.
Upvotes: 0
Reputation: 3638
I'm not sure what you mean by selecing cells. You can assign ids at table creation and select them later on:
cell.id = 'cell_' + (rowNum * boardSize + columnNum);
With this you can pick myNum
cells and select them:
numberOfCells = boardSize * boardSize;
// pick a random cell.
cellId = 'cell_' + Math.floor(Math.random() * numberOfCells);
document.getElementById(cellId);
I implemented that based on your fiddle here:
function createTable(size) {
var table, tr, td;
table = document.createElement('table');
for (var row = 0; row < size; ++row) {
tr = document.createElement('tr');
for (var col = 0; col < size; ++col) {
td = document.createElement('td');
td.id = 'cell_' + (row * size + col);
td.innerHTML = '0';
tr.appendChild(td);
}
table.appendChild(tr);
}
return table;
}
function selectRandomCells(size, count) {
var x, numCells, cellId, cell;
numCells = size * size;
x = 0;
while (x < count && x < numCells) {
cellId = 'cell_' + Math.floor(Math.random() * numCells);
cell = document.getElementById(cellId);
if (cell.classList.contains('selected')) {
// Already selected.
continue;
}
cell.classList.add('selected');
cell.innerHTML = '1';
x++;
}
}
function tableMatrix () {
var board, myNumb, myContainer, table;
// Form values.
board = document.forms["myForm"]["board"].value;;
myNumb = document.forms["myForm"]["myNumb"].value;
myContainer = document.getElementById('myContainer');
myContainer.innerHTML = '';
// Create table.
table = createTable(board);
myContainer.appendChild(table);
selectRandomCells(board, myNumb);
};
table {
width: 100%;
}
table, td {
border: 1px solid black;
}
td.selected {
background: #ccc;
}
<form name="myForm" method="post">
<label>Row & Columns Number</label>
<br/>
<input type="number" name="board" value="10" placeholder="Number of rows">
<br/><br/>
<label>Number of selected cells</label>
<br/>
<input type="number" name="myNumb" value="10" placeholder="Number of selected cells">
<br/><br/>
<input type="button" value="submit" onclick="tableMatrix()">
</form>
<div id="myContainer"></div>
Upvotes: 1
Reputation: 3124
Here is the script which generates , your desired patter:
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function fillCells(value, len) {
var arr = [];
for (var i = 0; i < len; i++) { if(i<value){
arr.push(1);}else{arr.push(0);}
}
shuffle(arr);
return arr;
}
function tableMatrix () {
//taking values from the form
var board = document.forms["myForm"]["board"].value;;
var myNumb = document.forms["myForm"]["myNumb"].value;
var zero = 0;
var one = 1;
var totalCell = board*board;
var cellValues = fillCells(myNumb, totalCell)
//finding myContainer
var myContainer = document.getElementById('myContainer');
myContainer.innerHTML = '';
//creating element <table>
var table = document.createElement('table');
table.style.width = '100%';
table.setAttribute('border', '1');
//creating element <tbody>
var tbody = document.createElement('tbody');
//generate random number
var x = 0;
//cells creation
for (var i = 0; i < board; i++) {
//creating <tr> element
var tr = document.createElement('tr');
for (var j = 0; j < board; j++) {
//creating <td> element
var td = document.createElement('td');
//put <td> after <tr> element
tr.appendChild(td);
td.setAttribute('class', 'tblCells');
td.innerHTML = '<span>'+cellValues[x]+'</span>';
x++;
}
//put <tr> after <tbody> element
tbody.appendChild(tr);
}
//put <tbody> after <table> element
table.appendChild(tbody);
//put <table> after <div> element
myContainer.appendChild(table);
}
here is the Bin
Upvotes: 2