-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailCli.js
More file actions
52 lines (46 loc) · 1.35 KB
/
mailCli.js
File metadata and controls
52 lines (46 loc) · 1.35 KB
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
var {sendSantaMail} = require('./mailSender');
const players = require('./players.json');
/**
* Shuffles an array randomly
* @param {Array} arr - array to shuffle
*/
function shuffle(arr) {
var i,j,temp;
for (i = arr.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
};
/**
* For the array of senders get the randomly generated recipients
* Function makes sure that the excluded pairs are not possible
* @param {Array} senders - list of senders
*/
function getRec(senders){
var rec = Array.from(senders)
var notFound=true
while(notFound){
rec = shuffle(rec)
notFound = false
for(var i=0;i< rec.length; i++){
if(senders[i] == rec[i]){
notFound = true
}
if(players[senders[i]].exclude == rec[i]){
notFound = true
}
}
}
return rec;
}
let senders = Object.keys(players);
let recepients = getRec(senders);
for(var i=0; i< senders.length; i++) {
var senderMail = players[senders[i]].email;
var senderName = players[senders[i]].firstname;
var recepientFullName = players[recepients[i]].firstname + " " + players[recepients[i]].lastname;
sendSantaMail(senderMail, senderName, recepientFullName);
}