我一直在努力争取如何创建一个带有一系列名称的函数,并将它们划分为 n 的团队数量,团队中的所有名称都是随机的。
我可以这样做:
var numBig = size of the array % number of teams
var smallTeamSize = Math.floor(size of the array % number of teams)
最大的团队应该只有一个名称,而不是其他团队。
首先numBig
个小组会有smallTeamSize + 1
个名称,剩下的小组会有smallTeamSize
个名称。
输入和输出应如下所示:
function randTeams(array, 3);
然后该函数可能(因为它是随机的)打印出以下内容:
Team 1: jimes, jim, joe
Team 2: jake, rob
Team 3: paps, richie
有什么建议吗?
我有一个随机化名称数组的函数:
var array = ['jim','jake','jimes','rob','paps','richie','joe'];
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * i);
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
var newarray =(shuffleArray(array));
我如何将新阵容变成小团队?
答案 0 :(得分:0)
我写这个是假设你想要一组名字,并从这些名字创建两个团队(虽然你的问题显然不明确):
function createTeams(names) {
var team1 = [],
n;
for (var i = 0, len = Math.floor(names.length / 2); i < len; i++) {
n = Math.floor(Math.random() * names.length);
team1.push(names[n]);
names.splice(n, 1);
}
return {
'team1': team1,
'team2': names
};
}
var teams = createTeams(['James', 'Bill', 'William', 'Jack', 'Steve']),
team1 = teams.team1,
team2 = teams.team2;
console.log(team1, team2); // ["Steve", "William"] ["James", "Bill", "Jack"]
编辑,包括一些有点混乱的创建任意数量的团队的方法:
function createTeams(names, numTeams) {
if (!names || typeof names == 'number'){
return false;
}
else {
numTeams = typeof numTeams == 'undefined' ? 2 : Math.abs(parseInt(numTeams,10));
var teams = {},
perTeam = Math.floor(names.length/numTeams),
n;
for (var i = 0; i < numTeams; i++){
teams[i] = [];
}
while (names.length - perTeam > 0){
for (var i = 0; i < numTeams; i++){
n = Math.floor(Math.random() * names.length);
if (names.length >= 0 && typeof names[n] !== 'undefined'){
teams[i].push(names[n]);
names.splice(n,1);
}
}
}
return teams;
}
}
var teams = createTeams(['James', 'Bill', 'William', 'Jack', 'Steve'], '5');
console.log(teams);
/*
{
'0' : ['Steve'],
'1' : ['James'],
'2' : ['Bill'],
'3' : ['Jack'],
'4' : ['William']
}
*/
参考文献:
答案 1 :(得分:0)
您可以将函数splitTeams添加到shuffleArray
function splitTeams(names) {
return {'team1': names.splice(0, Math.floor(names.length/2)),
'team2': names}
}
并像这样使用
var teams = splitTeams(shuffleArray(['jim','jake','jimes','rob','paps','richie']))
要建立多个团队,您可以使用splitTeams的这种变体:
function splitTeams(names, teams_count) {
var teams = [];
while (teams_count > 0) {
teams.push(names.splice(0, Math.floor(names.length/teams_count)))
teams_count--;
}
return teams
}
答案 2 :(得分:0)
一旦你拥有随机数组,就可以轻松地“将玩家交易”到你想要的任何数字:
function distributePlayers(names, numberOfTeams) {
var ret = {};
var teamCounter = 0;
for (var i = 0; i < names.length; ++i) {
if (!ret["array" + teamCounter]) {
ret["array" + teamCounter] = [];
}
ret["array" + teamCounter].push(names[i]);
if (++teamCounter == numberOfTeams) {
teamCounter = 0;
}
}
return ret;
}
然后您只需要:
var newArray = shuffleArray(array);
var arrays = distributePlayers(newArray, numberOfTeams);
内部arrays
将成员为array1,array2 ... arrayN。
工作Fiddle
<强>更新强>:
要遍历返回,遍历所有对象自己的属性,并在每个属性中加入数组以获得完整的团队:
for (var property in arrays) {
// check to make sure you didn't stumble on an inherited property
if (arrays.hasOwnProperty(property)) {
var str = property + ": " + arrays[property].join(", ");
console.log(str);
}
}
这将产生输出:
array0: richie, jimes, rob
array1: joe, jim
array2: paps, jake
如果您想重命名数组以反映团队名称,只需更改if
中for
循环内的distributePlayers
:
if (!ret["Team" + (teamCounter + 1)) {
ret["Team" + (teamCounter + 1));
}
ret["Team" + (teamCounter + 1)].push(names[i]);
这样,您的数组名称实际上将反映有用的信息。
答案 3 :(得分:0)
请参阅此代码,该代码会对数组进行混洗并对其进行切片
var names = ['jim','jake','jimes','rob','paps','richie'];
names.sort(function(){
return Math.round(Math.random()) - 0.5;
});
var noOfTeams = 3;
var totalTeams = Math.ceil(names.length/noOfTeams);
var i,j,temparray,chunk = totalTeams;
for (i=0,j=names.length; i<j; i+=chunk) {
temparray = names.slice(i,i+chunk);
console.log(temparray);
}
请参阅小提琴http://jsfiddle.net/abhiklpm/QDF5J/,在控制台中您将看到随机数组