×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Kale Waite
Added: Apr 5, 2017 3:30 PM
Views: 7
Tags: no tags
  1. var names = ["Gabe", "Morgan", "Aaron", "Josh", "Ben", "Miriam", "Evan", "Steve", "Dan", "Ben", "DanTwo", "Mike", "Andrea", "Tyson", "Melissa", "Jessie"];
  2.  
  3. function randomPerson(randomItem) {
  4.     return randomItem[Math.floor(Math.random() * randomItem.length)];
  5. }
  6.  
  7. function groupTeams(names, randomNumber) {
  8.     var arrayOfTeams = [];
  9.     var team = [];
  10.     var randomPersonVariable = '';
  11.  
  12.     while (names.length > 0) {
  13.  
  14.         for (var i = 0; team.length < randomNumber; i++) {
  15.             randomPersonVariable = randomPerson(names);
  16.             if (randomPersonVariable === undefined) {
  17.                 break;
  18.             }
  19.             team.push(randomPersonVariable);
  20.             names.splice(names.indexOf(randomPersonVariable), 1);
  21.         }
  22.         arrayOfTeams.push(team);
  23.         team = [];
  24.     }
  25. console.log(arrayOfTeams);
  26. }
  27.  
  28. groupTeams(names, 4);
  29.  
  30.