-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
61 lines (51 loc) · 1.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const STUDENTS = [
'Yetkin',
'Daniel',
'Tony G',
'Matt',
'Zuber',
'Kate',
'Jamal',
'Mel',
'Tom',
'Luke Sikuade',
'Phil',
'Tony B',
'Chris',
'Luke Speirs',
'Nicola',
'David',
'Yelena',
'Joe',
'Roland',
'Mariusz'
];
const GROUP_SIZE = 2;
// returns a random number between 0 and limit
function getRandomNumber(limit){
const random = Math.floor(Math.random() * limit);
return random;
}
// takes an array of names and returns a shuffled array of names
function shuffle(names){
const output = [];
while(names.length > 0){
const randomNumber = getRandomNumber(names.length);
const randomNameInArray = names.splice(randomNumber, 1)
const randomName = randomNameInArray[0];
output.push(randomName);
}
return output;
}
// takes an array of names and groups them into arrays of length size
function group(names, size){
const output = [];
while(names.length > 0){
const group = names.splice(0, size);
output.push(group);
}
return output;
}
const randomNames = shuffle(STUDENTS);
const randomGroups = group(randomNames, GROUP_SIZE);
console.log(randomGroups)