Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

分组 #3

Open
toplan opened this issue Apr 23, 2016 · 1 comment
Open

分组 #3

toplan opened this issue Apr 23, 2016 · 1 comment

Comments

@toplan
Copy link
Owner

toplan commented Apr 23, 2016

'use strict'

var arr = [1,2,3,4,5,6,7,8,9,0];
var group = [[],[],[],[]];

(function split(arr, group) {

  var size = group.length; // 分多少组
  var length = arr.length; // 人数总长度

  // 每次随机叫一个人,跳进一个组里,并从原数组里踢掉
  while(arr.length) {
    for(let i = 0; i < length; i++){ 
      let index = Math.random() * arr.length | 0;  // 随机
      group[i % size].push(arr.splice(index, 1)[0]);            // 踢掉并入组
    }
  }

  console.log(group);
})(arr, group);
@toplan
Copy link
Owner Author

toplan commented May 27, 2016

sofish/learn-js#8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant