0

我一直在努力研究如何创建一个函数,该函数采用一组名称并将它们分成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));

我将如何将 newarray 纳入更小的团队?

4

4 回答 4

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"]

JS 小提琴演示

编辑,包括创建任意数量的团队有点混乱的方法:

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']
    }
*/

JS 小提琴演示

参考:

于 2013-11-11T19:00:04.393 回答
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/,在控制台中您将看到随机数组

于 2013-11-11T19:44:03.833 回答
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。

工作小提琴

更新

要遍历返回,遍历对象自身的所有属性,并在每个属性中加入数组以获得完整的团队:

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内部:fordistributePlayers

if (!ret["Team" + (teamCounter + 1)) {
    ret["Team" + (teamCounter + 1));
}
ret["Team" + (teamCounter + 1)].push(names[i]);

这样,您的数组名称实际上将反映有用的信息。

更新的小提琴

于 2013-11-11T19:15:33.733 回答
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
}
于 2013-11-11T19:08:18.223 回答