我试图随机抽出52个元素并将它们放在一个由八组六组和一组五组组成的表中。数组中的元素是我想要放在表中的有问题的元素。 - http://jsfiddle.net/m5WF7/
有许多规则,但在实施这些规则之前,我宁愿让代码完全运行:
组A-H每个包含来自每个Pots 1-6的一个团队,而组I包含来自每个Pots 1-5的一个团队。 - 如果所有单元格都已填满,则遵守此规则。
出于电视转播的原因,英格兰,德国,意大利,西班牙和荷兰应分成6个小组。
出于政治原因,阿塞拜疆和亚美尼亚(由于纳戈尔诺 - 卡拉巴赫的争议地位)以及西班牙和直布罗陀(由于有争议的直布罗陀地位)不得在同一组中划出。
<HTML>
<HEAD>
<TITLE>Sample Page that Writes Out an HTML Table</TITLE>
</HEAD>
<BODY>
<SCRIPT Language="JavaScript">
var seed;
var potnum;
var pot1 = ["Spain", "Germany", "Netherlands", "Italy", "England", "Portugal", "Greece", "Russia", "Bosnia"];
var pot2 = ["Ukraine", "Croatia", "Sweden", "Denmark", "Switzerland", "Belgium", "Czech Republic", "Hungary", "Republic of Ireland"];
var pot3 = ['Serbia', 'Turkey', 'Slovenia', 'Israel', 'Norway', 'Slovakia', 'Romania', 'Austria', 'Poland'];
var pot4 = ['Montenegro', 'Armenia', 'Scotland', 'Finland', 'Latvia', 'Wales', 'Bulgaria', 'Estonia', 'Belarus'];
var pot5 = ['Iceland', 'Northern Ireland', 'Albania', 'Lithuania', 'Moldova', 'Macedonia', 'Azerbaijan', 'Georgia', 'Cyprus'];
var pot6 = ['Luxembourg', 'Kazakhstan', 'Liechtenstein', 'Faroe Islands', 'Malta', 'Andorra', 'San Marino', 'Gibraltar'];
var pots = [ 0, pot1 , pot2 , pot3 , pot4 , pot5 , pot6 ];
function sortpot( potnum )
{
var pot = pots[potnum];
var len = pot.length;
var rand = Math.random() * (pot.length);
rand = Math.floor(rand);
seed = pot[rand];
console.log(seed);
pot.splice(rand, 1);
pots[potnum] = pot;
return seed;
}
function add2table(potnum, seed) {
var pot = pots[potnum];
return pot[seed];
}
document.write('<table border="1" cellspacing="1" cellpadding="5">')
for(i = 1; i < 7; i++){
document.write('<tr>')
sortpot(i)
document.write('<td>' + i + '. </td>')
document.write('<td>' + i + '.' + add2table(i, 1) + '</td>')
document.write('<td>' + i + '.' + add2table(i, 2) +'</td>')
document.write('<td>' + i + '.' + add2table(i, 3) +'</td>')
document.write('<td>' + i + '.' + add2table(i, 4) +'</td>')
document.write('<td>' + i + '.' + add2table(i, 5) +'</td>')
document.write('<td>' + i + '.' + add2table(i, 6) +'</td>')
document.write('<td>' + i + '.' + add2table(i, 7) +'</td>')
document.write('<td>' + i + '.' + add2table(i, 8) +'</td>')
if(i < 6) {
document.write('<td>' + i + '.' + add2table(i, 9) +'</td>')
}
}
document.write('</tr>')
document.write('</table>')
</SCRIPT>
<button onclick="sortpot(1);">sort Pot 1</button>
<button onclick="sortpot(2);">sort Pot 2</button>
<button onclick="sortpot(3);">sort Pot 3</button>
<button onclick="sortpot(4);">sort Pot 4</button>
<button onclick="sortpot(5);">sort Pot 5</button>
<button onclick="sortpot(6);">sort Pot 6</button>
</BODY>
</HTML
目前我正在获取一个表,其中数组中的团队填充列,但最后两列和第7列的单元格6除外,这可能是因为Uncaught TypeError。我如何完成整个表格?
我打算使用按钮激活代码,但是当我打开小提琴或网页时代码会执行。我现在只想用团队填补所有细胞。
答案 0 :(得分:0)
我们仍然很难准确理解你要做的事情,但是我做了一个小提琴,它会记录你的国家名单,并会在点击后从适当的底池中生成一个随机的国家列表并将其放入连续在一张桌子里。希望它有所帮助。
HTML:
<button onclick="sortpot(1);">sort Pot 1</button>
<button onclick="sortpot(2);">sort Pot 2</button>
<button onclick="sortpot(3);">sort Pot 3</button>
<button onclick="sortpot(4);">sort Pot 4</button>
<button onclick="sortpot(5);">sort Pot 5</button>
<button onclick="sortpot(6);">sort Pot 6</button>
<table id="myTable" border="1" cellspacing="1" cellpadding="5">
</table>
JS:
var countryCollection = [
["Spain", "Germany", "Netherlands", "Italy", "England", "Portugal", "Greece", "Russia", "Bosnia"],
["Ukraine", "Croatia", "Sweden", "Denmark", "Switzerland", "Belgium", "Czech Republic", "Hungary", "Republic of Ireland"],
['Serbia', 'Turkey', 'Slovenia', 'Israel', 'Norway', 'Slovakia', 'Romania', 'Austria', 'Poland'],
['Montenegro', 'Armenia', 'Scotland', 'Finland', 'Latvia', 'Wales', 'Bulgaria', 'Estonia', 'Belarus'],
['Iceland', 'Northern Ireland', 'Albania', 'Lithuania', 'Moldova', 'Macedonia', 'Azerbaijan', 'Georgia', 'Cyprus'],
['Luxembourg', 'Kazakhstan', 'Liechtenstein', 'Faroe Islands', 'Malta', 'Andorra', 'San Marino', 'Gibraltar']
];
function sortpot(listNum){
var numCountries = countryCollection[listNum-1].length,//cache it because it will change as we splice it
i,
randomizedList = [],
randNum;
for(i = 0; i < numCountries; i += 1){
randNum = Math.floor(Math.random() * countryCollection[listNum-1].length);
randomizedList.push(countryCollection[listNum-1][randNum]);
countryCollection[listNum-1].splice(randNum, 1);
}
console.log(randomizedList);
writeRow(randomizedList);
}
function writeRow(randomizedList){
var row = document.createElement('tr'),
cell;
randomizedList.forEach(function(ele){
cell = document.createElement('td');
cell.innerHTML = ele;
row.appendChild(cell);
});
console.log(row);
document.getElementById('myTable').appendChild(row);
}