Menu
I create a 30 cell table
0000000
0000000
0000000
0000000
00
using date function I am able to get the day and date.
I am assuming first day(1st) is match with first cell , I will first mark out saturday, so its 6,13,20,27, give it a color black.
00000×0
00000×0
00000×0
00000×0
00
with above mark out, I remain 26 cell (30 – 4) I have 5 color (red,blue,green,yellow,pink) red – 3 (a fixed number) blue – 4(a fixed number) green – 5(a fixed number) yellow – 6(a fixed number) pink – 8(a fixed number)
I have to random these 26 color to fit the remaining boxes
What is the correct way to achieve this?
[code=php]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Random Cell Colors </title>
<style type="text/css">
td { width:1em; }
</style>
</head>
<body>
<table id="tbl" border='1'>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<button onclick=initTblColors()>Random Test</button>
<script type="text/javascript">
// From: http://www.codingforums.com/javascript-programming/375801-playing-table-cell-color.html
var tblColors = ['red','red','red',
'blue','blue','blue','blue',
'green','green','green','green','green',
'yellow','yellow','yellow','yellow','yellow','yellow',
'pink','pink','pink','pink','pink','pink','pink','pink',
'black','black','black','black'
];
Number.prototype.random = function() { return Math.floor(Math.random() * this); }
function initTblColors() {
var sel = document.getElementById('tbl').getElementsByTagName('td');
for (var i=0; i<sel.length; i++) { sel[i].style.backgroundColor = tblColors[i]; }
var rc, tc1, tc2;
for (var i=0; i<26; i++) {
rc = (26).random();
tc1 = sel[i].style.backgroundColor;
tc2 = sel[rc].style.backgroundColor;
sel[rc].style.backgroundColor = tc1;
sel[i].style.backgroundColor = tc2;
}
var sat = [5,12,19]; // sat list
for (var i=0; i<sat.length; i++) {
rc = sat[i];
tc1 = sel[rc].style.backgroundColor;
tc2 = sel[27+i].style.backgroundColor;
sel[27+i].style.backgroundColor = tc1;
sel[rc].style.backgroundColor = tc2;
}
}
initTblColors()
</script>
</body>
</html>
[/code]
One (of many) possible ways to achieve this...
[code=php]
[/QUOTE]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Random Cell Colors </title>
<style type="text/css">
td { width:1em; }
</style>
</head>
<body>
<table id="tbl" border='1'>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
</tr>
</table>
<button onclick=initTblColors()>Random Test</button>
<script type="text/javascript">
// From: http://www.codingforums.com/javascript-programming/375801-playing-table-cell-color.html
var tblColors = ['red','red','red',
'blue','blue','blue','blue',
'green','green','green','green','green',
'yellow','yellow','yellow','yellow','yellow','yellow',
'pink','pink','pink','pink','pink','pink','pink','pink',
'black','black','black','black'
];
Number.prototype.random = function() { return Math.floor(Math.random() * this); }
function initTblColors() {
var sel = document.getElementById('tbl').getElementsByTagName('td');
for (var i=0; i<sel.length; i++) { sel[i].style.backgroundColor = tblColors[i]; }
var rc, tc1, tc2;
for (var i=0; i<26; i++) {
rc = (26).random();
tc1 = sel[i].style.backgroundColor;
tc2 = sel[rc].style.backgroundColor;
sel[rc].style.backgroundColor = tc1;
sel[i].style.backgroundColor = tc2;
}
var sat = [5,12,19]; // sat list
for (var i=0; i<sat.length; i++) {
rc = sat[i];
tc1 = sel[rc].style.backgroundColor;
tc2 = sel[27+i].style.backgroundColor;
sel[27+i].style.backgroundColor = tc1;
sel[rc].style.backgroundColor = tc2;
}
}
initTblColors()
</script>
</body>
</html>
[/code]
Hi JMRKER
Thank you very much.
For the tblColors array , is it possible to use variable(contains a digit generated elsewhere where) instead of hardcoding.
For example : var redAmt = 3;
var tblColors = [ 'red' = redAmt,'blue' ][/QUOTE]
Yes, it might be possible to use a variable, but not the way you proposed.
My main questions is why? What is the purpose of the change request?
What is it that you are really trying to accomplish?
Or how does the change work with some other (un-specified) requirement?[/QUOTE]
3) Lastly there a export button. I use a table as I would like to export to excel.
Hopefully the colors and value of each cell is exported successfully.
My excel codes are not working in internet explorer.
[/quote]
If you are going to be pasting this in to Excel, why are you not creating this in Excel in the first place?[/QUOTE]
A few more questions.
1. The user only enters numbers for the red and blue colors? Are the other 3 are randomly assigned based upon the number of open cells left? Is the user allowed to enter 0 (zero) as the number cells of a particular color to display?
2. What if the user enters more numbers (1 to 5 color values) than there are cells left? Or more that will allow any some or none of the extra random colors?
3. Javascript will not export directly to Excel. Do you wish to create a CSV or text file to import into Excel? What is the format of the Excel file you wish to create?
What do you mean by the colors and value of each cell? If you pass the color, what is the value of the cell? If you pass the value, what is the color? Or how does the value differ from the color of the cell when pasted to the Excel work page.
As you can see, there is not much use in creating a script until all the variables are defined. You should take more time with that logic of the program than actual coding.
I think I understand what you want displayed (maybe) but I am still confused about the purpose. What does the days of the week (0...6) have to do with your position requirement of the table display of 1 to 30? And what about 31 days in some of the months? Is the last day ignored in this case?
Note: Arrays in JS are referenced from 0 [zero] to the number of cells. You can call the cells anything you desire (1 to 30 or 101 to 130 or whatever), but the easier way to reference a cell within the program is from 0 to 29, especially for calculation and display purposes.[/QUOTE]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Random Cell Colors </title>
<style type="text/css">
td { width:1em; }
/* td:nth-child(6){background-color: black} */
#ad { font-size:30px; width:600px;}
</style>
</head>
<body>
<table id="tablo" border='1'>
<tr>
<th>pazartesi</th>
<th>sal&#305;</th>
<th>çar&#351;amba</th>
<th>per&#351;embe</th>
<th>cuma</th>
<th>cumartesi</th>
<th>pazar</th>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
</tr>
</table>
<p><label style="color:lime;"><button onclick=start()>Random Test</button></p>
<p><label style="color:#b08;">boya(<label style="color:red">numberOfRed</label>,<label style="color:blue">numberOfBlue</label>,<label style="color:green">numberOfGreen</label>,<label style="color:yellow">numberOfYellow</label>,<label style="color:pink">numberOfPink</label>,<label style="color:black">numberOfBlack,numberOfBox)</label></p>
<p><input type="text" id="ad" value="boya(3,4,5,6,8,4,30)" ></p>
<script type="text/javascript">
//Bu kodda sadece siyah ve mavi olan alanlar boyand&#305;.
Number.prototype.random = function() { return Math.floor(Math.random() * this); }
function boya(r,b,g,y,p,bl,box){
var el = document.getElementById('tablo').getElementsByTagName('td');
var i, B=[], D=[];
for(i=0, c = 5; i<box; i++){
if( (i%7)== 5){ el[i].style.backgroundColor = "black"; } // cumartesi sütununu siyah renkli yapal&#305;m.
if( (i%7)!= 5){ B[B.length]=i; } // cumartesi olmayan sütunlardaki kutular&#305; diziye alal&#305;m
if( (i%7) < 4) {D[D.length]=i; } // ilk 4 sütüna mavi renk yerle&#351;tirilecek. Bu sütunlardaki kutular&#305;n numaralar&#305;n&#305; bir dizide toplad&#305;k.
}
//alert(D);
// mavi rengi kutulara ata
var n;
for(i=0; i< b; i++){
n = (D.length-1).random(); // rastgele bir say&#305; elde et
el[ D[n] ].style.backgroundColor="blue"; // kutuya renk ata
// http://www.w3schools.com/jsref/jsref_splice.asp
alert( "n= "+n+" nD= "+D+ " nD[n]= "+D[n] + "n D.length= "+D.length);
D.splice(n,1);
}
var i, toplam=0;
var L = arguments.length;
for(i=0; i< (L-1); i++){
toplam = toplam +arguments[i];
}
// alert(i);
if(toplam != arguments[i]){
//alert(" renklerin say&#305;s&#305;n&#305;n toplam&#305; != kutular&#305;n say&#305;s&#305;");
}
if(toplam == arguments[i]){
//alert(" renklerin say&#305;s&#305;n&#305;n toplam&#305; = kutular&#305;n say&#305;s&#305;");
}
}
function start(){
var ad= document.getElementById('ad');
Function(ad.value)();
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Random Cell Colors </title>
<style type="text/css">
td { width:1em; }
/* td:nth-child(6){background-color: black} */
#ad { font-size:30px; width:600px;}
</style>
</head>
<body>
<table id="tablo" border='1'>
<tr>
<th>pazartesi</th>
<th>sal&#305;</th>
<th>çar&#351;amba</th>
<th>per&#351;embe</th>
<th>cuma</th>
<th>cumartesi</th>
<th>pazar</th>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
</tr>
</table>
<p><label style="color:lime;"><button onclick=start()>Random Test</button></p>
<p><label style="color:#b08;">boya(<label style="color:red">numberOfRed</label>,<label style="color:blue">numberOfBlue</label>,<label style="color:green">numberOfGreen</label>,<label style="color:yellow">numberOfYellow</label>,<label style="color:pink">numberOfPink</label>,<label style="color:black">numberOfBlack,numberOfBox)</label></p>
<p><input type="text" id="ad" value="boya(3,4,5,6,8,4,30)" ></p>
<script type="text/javascript">
//Bu kodda sadece siyah ve mavi olan alanlar boyand&#305;.
Number.prototype.random = function() { return Math.floor(Math.random() * this); }
function boya(r,b,g,y,p,bl,box){
var el = document.getElementById('tablo').getElementsByTagName('td');
var i, B=[], D=[];
for(i=0, c = 5; i<box; i++){
if( (i%7)== 5){ el[i].style.backgroundColor = "black"; } // cumartesi sütununu siyah renkli yapal&#305;m.
if( (i%7)!= 5){ B[B.length]=i; } // cumartesi olmayan sütunlardaki kutular&#305; diziye alal&#305;m
if( (i%7) < 4) {D[D.length]=i; } // ilk 4 sütüna mavi renk yerle&#351;tirilecek. Bu sütunlardaki kutular&#305;n numaralar&#305;n&#305; bir dizide toplad&#305;k.
}
//alert(D);
// mavi rengi kutulara ata
var n;
for(i=0; i< b; i++){
n = (D.length-1).random(); // rastgele bir say&#305; elde et
el[ D[n] ].style.backgroundColor="blue"; // kutuya renk ata
// http://www.w3schools.com/jsref/jsref_splice.asp
alert( "n= "+n+" nD= "+D+ " nD[n]= "+D[n] + "n D.length= "+D.length);
D.splice(n,1);
}
var i, toplam=0;
var L = arguments.length;
for(i=0; i< (L-1); i++){
toplam = toplam +arguments[i];
}
// alert(i);
if(toplam != arguments[i]){
//alert(" renklerin say&#305;s&#305;n&#305;n toplam&#305; != kutular&#305;n say&#305;s&#305;");
}
if(toplam == arguments[i]){
//alert(" renklerin say&#305;s&#305;n&#305;n toplam&#305; = kutular&#305;n say&#305;s&#305;");
}
}
function start(){
var ad= document.getElementById('ad');
Function(ad.value)();
}
</script>
</body>
</html>
[/QUOTE]
[code=php]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Random Cell Colors </title>
<style type="text/css">
td { width:1em; }
</style>
</head>
<body>
Enter amounts to total of 26<br>
<div id="cAmounts" style="width:25%">
<div style="float:left">
<input id="cRed" type="text" value="3" size="2"> Red <br>
<input id="cBlue" type="text" value="4" size="2"> Blue <br>
<input id="cGreen" type="text" value="5" size="2"> Green <br>
</div>
<div style="float:right">
<input id="cYellow" type="text" value="6" size="2"> Yellow <br>
<input id="cPink" type="text" value="8" size="2"> Pink <br>
<input id="cTotals" type="text" value="" size="2" readonly>
<button onclick="summate()"> Total </button>
</div>
</div>
<br style="clear:both"> <p>
<table id="tbl" border='1'>
<tr>
<td> </td> <td> </td> <td> </td> <td> </td>
<td> </td> <td> </td> <td> </td>
</tr>
<tr>
<td> </td> <td> </td> <td> </td> <td> </td>
<td> </td> <td> </td> <td> </td>
</tr>
<tr>
<tr>
<td> </td> <td> </td> <td> </td> <td> </td>
<td> </td> <td> </td> <td> </td>
</tr>
<tr>
<td> </td> <td> </td> <td> </td> <td> </td>
<td> </td> <td> </td> <td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<button onclick=initTblColors()>Random Test</button>
<script type="text/javascript">
// From: http://www.codingforums.com/javascript-programming/375801-playing-table-cell-color.html
var tblColors = [];
Number.prototype.random = function() { return Math.floor(Math.random() * this); }
function initTblColors() {
var sel = document.getElementById('tbl').getElementsByTagName('td');
for (var i=0; i<sel.length; i++) { sel[i].style.backgroundColor = tblColors[i]; }
var rc, tc1, tc2;
for (var i=0; i<26; i++) {
rc = (26).random();
tc1 = sel[i].style.backgroundColor;
tc2 = sel[rc].style.backgroundColor;
sel[rc].style.backgroundColor = tc1;
sel[i].style.backgroundColor = tc2;
}
var sat = [5,12,19]; // sat list
for (var i=0; i<sat.length; i++) {
rc = sat[i];
tc1 = sel[rc].style.backgroundColor;
tc2 = sel[27+i].style.backgroundColor;
sel[27+i].style.backgroundColor = tc1;
sel[rc].style.backgroundColor = tc2;
}
} summate(); // initTblColors();
function summate() {
var sel = document.getElementById('cAmounts').getElementsByTagName('input');
var sum = 0;
for (var i=0; i<sel.length-1; i++) { sum += sel[i].value * 1; }
document.getElementById('cTotals').value = sum;
if (sum != 26) { alert('Invalid Total'); return; }
var colorPicks = ['red','lightblue','lime','yellow','pink']; // or ['red','blue','green','yellow','pink']
tblColors.length = 0;
var cnt;
for (var i=0; i<sel.length-1; i++) {
cnt = sel[i].value * 1;
for (var j=0; j<cnt; j++) {
tblColors.push(colorPicks[i]);
}
}
for (var i=0; i<4; i++) { tblColors.push('black'); }
initTblColors();
}
</script>
</body>
</html>
[/code]
No conversion to Excel in this version but is this heading in the right direction for your query?
[code=php]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Random Cell Colors </title>
<style type="text/css">
td { width:1em; }
</style>
</head>
<body>
Enter amounts to total of 26<br>
<div id="cAmounts" style="width:25%">
<div style="float:left">
<input id="cRed" type="text" value="3" size="2"> Red <br>
<input id="cBlue" type="text" value="4" size="2"> Blue <br>
<input id="cGreen" type="text" value="5" size="2"> Green <br>
</div>
<div style="float:right">
<input id="cYellow" type="text" value="6" size="2"> Yellow <br>
<input id="cPink" type="text" value="8" size="2"> Pink <br>
<input id="cTotals" type="text" value="" size="2" readonly>
<button onclick="summate()"> Total </button>
</div>
</div>
<br style="clear:both"> <p>
<table id="tbl" border='1'>
<tr>
<td>*</td> <td>*</td> <td>*</td> <td>*</td>
<td>*</td> <td>*</td> <td>*</td>
</tr>
<tr>
<td>*</td> <td>*</td> <td>*</td> <td>*</td>
<td>*</td> <td>*</td> <td>*</td>
</tr>
<tr>
<tr>
<td>*</td> <td>*</td> <td>*</td> <td>*</td>
<td>*</td> <td>*</td> <td>*</td>
</tr>
<tr>
<td>*</td> <td>*</td> <td>*</td> <td>*</td>
<td>*</td> <td>*</td> <td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
</tr>
</table>
<button onclick=initTblColors()>Random Test</button>
<script type="text/javascript">
// From: http://www.codingforums.com/javascript-programming/375801-playing-table-cell-color.html
var tblColors = [];
Number.prototype.random = function() { return Math.floor(Math.random() * this); }
function initTblColors() {
var sel = document.getElementById('tbl').getElementsByTagName('td');
for (var i=0; i<sel.length; i++) { sel[i].style.backgroundColor = tblColors[i]; }
var rc, tc1, tc2;
for (var i=0; i<26; i++) {
rc = (26).random();
tc1 = sel[i].style.backgroundColor;
tc2 = sel[rc].style.backgroundColor;
sel[rc].style.backgroundColor = tc1;
sel[i].style.backgroundColor = tc2;
}
var sat = [5,12,19]; // sat list
for (var i=0; i<sat.length; i++) {
rc = sat[i];
tc1 = sel[rc].style.backgroundColor;
tc2 = sel[27+i].style.backgroundColor;
sel[27+i].style.backgroundColor = tc1;
sel[rc].style.backgroundColor = tc2;
}
} summate(); // initTblColors();
function summate() {
var sel = document.getElementById('cAmounts').getElementsByTagName('input');
var sum = 0;
for (var i=0; i<sel.length-1; i++) { sum += sel[i].value * 1; }
document.getElementById('cTotals').value = sum;
if (sum != 26) { alert('Invalid Total'); return; }
var colorPicks = ['red','lightblue','lime','yellow','pink']; // or ['red','blue','green','yellow','pink']
tblColors.length = 0;
var cnt;
for (var i=0; i<sel.length-1; i++) {
cnt = sel[i].value * 1;
for (var j=0; j<cnt; j++) {
tblColors.push(colorPicks[i]);
}
}
for (var i=0; i<4; i++) { tblColors.push('black'); }
initTblColors();
}
</script>
</body>
</html>
[/code]
See what you can come up with concerning the conversion as I still do not completely understand your requirements about this topic. Relates to the purpose (which I am still confused about) of the script or it's intended usage. ?[/QUOTE]
No conversion to Excel in this version but is this heading in the right direction for your query?
[code=php]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Random Cell Colors </title>
<style type="text/css">
td { width:1em; }
</style>
</head>
<body>
Enter amounts to total of 26<br>
<div id="cAmounts" style="width:25%">
<div style="float:left">
<input id="cRed" type="text" value="3" size="2"> Red <br>
<input id="cBlue" type="text" value="4" size="2"> Blue <br>
<input id="cGreen" type="text" value="5" size="2"> Green <br>
</div>
<div style="float:right">
<input id="cYellow" type="text" value="6" size="2"> Yellow <br>
<input id="cPink" type="text" value="8" size="2"> Pink <br>
<input id="cTotals" type="text" value="" size="2" readonly>
<button onclick="summate()"> Total </button>
</div>
</div>
<br style="clear:both"> <p>
<table id="tbl" border='1'>
<tr>
<td>*</td> <td>*</td> <td>*</td> <td>*</td>
<td>*</td> <td>*</td> <td>*</td>
</tr>
<tr>
<td>*</td> <td>*</td> <td>*</td> <td>*</td>
<td>*</td> <td>*</td> <td>*</td>
</tr>
<tr>
<tr>
<td>*</td> <td>*</td> <td>*</td> <td>*</td>
<td>*</td> <td>*</td> <td>*</td>
</tr>
<tr>
<td>*</td> <td>*</td> <td>*</td> <td>*</td>
<td>*</td> <td>*</td> <td>*</td>
</tr>
<tr>
<td>*</td>
<td>*</td>
</tr>
</table>
<button onclick=initTblColors()>Random Test</button>
<script type="text/javascript">
// From: http://www.codingforums.com/javascript-programming/375801-playing-table-cell-color.html
var tblColors = [];
Number.prototype.random = function() { return Math.floor(Math.random() * this); }
function initTblColors() {
var sel = document.getElementById('tbl').getElementsByTagName('td');
for (var i=0; i<sel.length; i++) { sel[i].style.backgroundColor = tblColors[i]; }
var rc, tc1, tc2;
for (var i=0; i<26; i++) {
rc = (26).random();
tc1 = sel[i].style.backgroundColor;
tc2 = sel[rc].style.backgroundColor;
sel[rc].style.backgroundColor = tc1;
sel[i].style.backgroundColor = tc2;
}
var sat = [5,12,19]; // sat list
for (var i=0; i<sat.length; i++) {
rc = sat[i];
tc1 = sel[rc].style.backgroundColor;
tc2 = sel[27+i].style.backgroundColor;
sel[27+i].style.backgroundColor = tc1;
sel[rc].style.backgroundColor = tc2;
}
} summate(); // initTblColors();
function summate() {
var sel = document.getElementById('cAmounts').getElementsByTagName('input');
var sum = 0;
for (var i=0; i<sel.length-1; i++) { sum += sel[i].value * 1; }
document.getElementById('cTotals').value = sum;
if (sum != 26) { alert('Invalid Total'); return; }
var colorPicks = ['red','lightblue','lime','yellow','pink']; // or ['red','blue','green','yellow','pink']
tblColors.length = 0;
var cnt;
for (var i=0; i<sel.length-1; i++) {
cnt = sel[i].value * 1;
for (var j=0; j<cnt; j++) {
tblColors.push(colorPicks[i]);
}
}
for (var i=0; i<4; i++) { tblColors.push('black'); }
initTblColors();
}
</script>
</body>
</html>
[/code]
See what you can come up with concerning the conversion as I still do not completely understand your requirements about this topic. Relates to the purpose (which I am still confused about) of the script or it's intended usage. ?[/QUOTE]
[CODE]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Random Cell Colors </title>
<style type="text/css">
td { width:1em; }
</style>
</head>
<body>
<table id="tbl" border='1'>
<tbody>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td class="twentyNine">x</td>
<td class="twentyNine">x</td>
<td class="twentyNine">x</td>
<td class="thirty">x</td>
<td class="thirty">x</td>
<td class="thirty">x</td>
<td class="thirty-one">x</td>
<td class="thirty-one">x</td>
<td class="thirty-one">x</td>
</tr>
<tr>
<td class="twentyNine">x</td>
<td class="twentyNine" >x</td>
<td class="twentyNine" >x</td>
<td class="thirty">x</td>
<td class="thirty">x</td>
<td class="thirty">x</td>
<td class="thirty-one">x</td>
<td class="thirty-one">x</td>
<td class="thirty-one">x</td>
</tr>
<tr>
<td class="twentyNine" >r</td>
<td class="twentyNine" >r</td>
<td class="twentyNine" >r</td>
<td class="thirty">r</td>
<td class="thirty">r</td>
<td class="thirty">r</td>
<td class="thirty-one">r</td>
<td class="thirty-one">r</td>
<td class="thirty-one">r</td>
</tr>
</tbody></table>
<button onclick=initTblColors()>Random Test</button>
<script type="text/javascript">
var tblColors = [];
Number.prototype.random = function() { return Math.floor(Math.random() * this); }
var sat = [17,34,51,68,85,103,120,137,154,171,188,205]; // fixed spot - 12 black block out
// as the number of boxes increase, I treat 9 boxes as 1, so in this case there are 31 boxes, total 31 x 9 = 279 boxes
var noOfBox = 29; // user can change 29 ,30, 31
function initTblColors() {
if (noOfBox == 29)
{
// $(".twentyNine").removeClass("twentyNine");
}else if (noOfBox == 30)
{
$(".twentyNine").removeClass("twentyNine");
$(".thirty").removeClass("thirty");
}else if (noOfBox == 31)
{
$(".twentyNine").removeClass("twentyNine");
$(".thirty").removeClass("thirty");
$(".thirty-one").removeClass("thirty-one");
}
var spans = document.getElementById('tbl').getElementsByTagName('td'); // Unsure what to type here , I want it to target all td with no class name of tbl label
var ColorRemainWithoutBlack = parseInt(spans.length) - 12;
for (var i = 0; i < spans.length; i++) {
spans[i].style.backgroundColor = tblColors[i]; }
var rc, tc1, tc2;
for (var j=0; j<ColorRemainWithoutBlack; j++) {
rc = (ColorRemainWithoutBlack).random();
tc1 = spans[j].style.backgroundColor;
tc2 = spans[rc].style.backgroundColor;
spans[rc].style.backgroundColor = tc1;
spans[j].style.backgroundColor = tc2;
}
for (var k=0; k<sat.length; k++) {
rc = sat[k];
tc1 = spans[rc].style.backgroundColor;
tc2 = spans[ColorRemainWithoutBlack+1+k].style.backgroundColor;
spans[ColorRemainWithoutBlack+1+ k].style.backgroundColor = tc1;
spans[rc].style.backgroundColor = tc2;
}
}summate(); // initTblColors();
function summate() {
var colorPicks = ['red','lightblue','lime','yellow','pink']; // or ['red','blue','green','yellow','pink']
tblColors.length = 0;
var cnt;
var cnt2;
var cnt3;
var cnt4;
var cnt5;
var cnt = 19; // value is generated, not fixed
var cnt2 = 34;// value is generated, not fixed
var cnt3 = 17;// value is generated, not fixed
var cnt4 = 42;// value is generated, not fixed
var cnt5 = 102;// value is generated, not fixed
for (var j=0; j<cnt; j++) {
tblColors.push(colorPicks[0]);
}
for (var k=0; k<cnt2; k++) {
tblColors.push(colorPicks[1]);
}
for (var j=0; j<cnt3; j++) {
tblColors.push(colorPicks[2]);
}
for (var j=0; j<cnt4; j++) {
tblColors.push(colorPicks[3]);
}
for (var j=0; j<cnt5; j++) {
tblColors.push(colorPicks[4]);
}
for (var i=0; i<sat.length; i++) { tblColors.push('black');} // 12 black
// remaining 53 red, to be generated within only the 'r' spots
initTblColors();
}
</script>
</body>
</html> [/CODE]
Hi Guys,
I have clean up and uploaded here instead. I am almost there with the results.
1) Fixed 12 black on "b" spot
2) 53 red, to be generated within only the 'r' spots ('r' spot is every 3 row, first 15 box)
3) unfilled 'r' spots + rest of unfilled box random (19,34,17,42,102 each different color)
53,19,34,17,42,102 are values in variable, could be change sometime down the road next time but total in final will be matching total box.
total box is variable too, could be change sometime down the road next time, can be 261(29x9), 270(30x9), 279(31x9).
double check (for reference only)
12 + 53 + 19 + 34 + 17 + 42 + 102 = 279 total boxes (31x9) just nice.
[CODE]
[/QUOTE]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Random Cell Colors </title>
<style type="text/css">
td { width:1em; }
</style>
</head>
<body>
<table id="tbl" border='1'>
<tbody>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>r</td>
<td>x</td>
<td>x</td>
<td>b</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td class="twentyNine">x</td>
<td class="twentyNine">x</td>
<td class="twentyNine">x</td>
<td class="thirty">x</td>
<td class="thirty">x</td>
<td class="thirty">x</td>
<td class="thirty-one">x</td>
<td class="thirty-one">x</td>
<td class="thirty-one">x</td>
</tr>
<tr>
<td class="twentyNine">x</td>
<td class="twentyNine" >x</td>
<td class="twentyNine" >x</td>
<td class="thirty">x</td>
<td class="thirty">x</td>
<td class="thirty">x</td>
<td class="thirty-one">x</td>
<td class="thirty-one">x</td>
<td class="thirty-one">x</td>
</tr>
<tr>
<td class="twentyNine" >r</td>
<td class="twentyNine" >r</td>
<td class="twentyNine" >r</td>
<td class="thirty">r</td>
<td class="thirty">r</td>
<td class="thirty">r</td>
<td class="thirty-one">r</td>
<td class="thirty-one">r</td>
<td class="thirty-one">r</td>
</tr>
</tbody></table>
<button onclick=initTblColors()>Random Test</button>
<script type="text/javascript">
var tblColors = [];
Number.prototype.random = function() { return Math.floor(Math.random() * this); }
var sat = [17,34,51,68,85,103,120,137,154,171,188,205]; // fixed spot - 12 black block out
// as the number of boxes increase, I treat 9 boxes as 1, so in this case there are 31 boxes, total 31 x 9 = 279 boxes
var noOfBox = 29; // user can change 29 ,30, 31
function initTblColors() {
if (noOfBox == 29)
{
// $(".twentyNine").removeClass("twentyNine");
}else if (noOfBox == 30)
{
$(".twentyNine").removeClass("twentyNine");
$(".thirty").removeClass("thirty");
}else if (noOfBox == 31)
{
$(".twentyNine").removeClass("twentyNine");
$(".thirty").removeClass("thirty");
$(".thirty-one").removeClass("thirty-one");
}
var spans = document.getElementById('tbl').getElementsByTagName('td'); // Unsure what to type here , I want it to target all td with no class name of tbl label
var ColorRemainWithoutBlack = parseInt(spans.length) - 12;
for (var i = 0; i < spans.length; i++) {
spans[i].style.backgroundColor = tblColors[i]; }
var rc, tc1, tc2;
for (var j=0; j<ColorRemainWithoutBlack; j++) {
rc = (ColorRemainWithoutBlack).random();
tc1 = spans[j].style.backgroundColor;
tc2 = spans[rc].style.backgroundColor;
spans[rc].style.backgroundColor = tc1;
spans[j].style.backgroundColor = tc2;
}
for (var k=0; k<sat.length; k++) {
rc = sat[k];
tc1 = spans[rc].style.backgroundColor;
tc2 = spans[ColorRemainWithoutBlack+1+k].style.backgroundColor;
spans[ColorRemainWithoutBlack+1+ k].style.backgroundColor = tc1;
spans[rc].style.backgroundColor = tc2;
}
}summate(); // initTblColors();
function summate() {
var colorPicks = ['red','lightblue','lime','yellow','pink']; // or ['red','blue','green','yellow','pink']
tblColors.length = 0;
var cnt;
var cnt2;
var cnt3;
var cnt4;
var cnt5;
var cnt = 19; // value is generated, not fixed
var cnt2 = 34;// value is generated, not fixed
var cnt3 = 17;// value is generated, not fixed
var cnt4 = 42;// value is generated, not fixed
var cnt5 = 102;// value is generated, not fixed
for (var j=0; j<cnt; j++) {
tblColors.push(colorPicks[0]);
}
for (var k=0; k<cnt2; k++) {
tblColors.push(colorPicks[1]);
}
for (var j=0; j<cnt3; j++) {
tblColors.push(colorPicks[2]);
}
for (var j=0; j<cnt4; j++) {
tblColors.push(colorPicks[3]);
}
for (var j=0; j<cnt5; j++) {
tblColors.push(colorPicks[4]);
}
for (var i=0; i<sat.length; i++) { tblColors.push('black');} // 12 black
// remaining 53 red, to be generated within only the 'r' spots
initTblColors();
}
</script>
</body>
</html> [/CODE]
I really don't know what the purpose of this exercise is.
The requirements seem to change every other post between user selected color displays and static displays.
Your displays of 'x', 'r', 'b', etc don't match the background colors assigned.
Until you can tell me what we are trying to accomplish, I'll be out of the loop for awhile.
I'll watch what others suggest if they can understand the problem better.
Sorry, I'm just not good at programming when the requirements are not well defined.
⭕([/QUOTE]
0.1.9 — BETA 5.17