-
Notifications
You must be signed in to change notification settings - Fork 8
/
level2.js
65 lines (44 loc) · 1.92 KB
/
level2.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
/*
Level 2
Part one:
Look at the assignSecretSanta function, I've started this for you, you can choose to use what I've given you
or try again for yourself. This function wants to take a shuffleArray and check that no one gets themself, if they do
run shuffleArray again, and check again. Once no one has themselves return this combination.
Before you start see what happens if you give an array of names as the parameter to the function shuffleArray.
Part two:
Once you've done this edit the function printSecretSanta so it console.logs a list of who has who for Secret Santa.
Part three:
If you finish this try making a Secret Santa class that contains data about the secret santa for example who is involved,
who has who, how much can be spent what some people might like as gifts. You can include the function you've already
created as a method in this class to console.logs who has who.
*/
const shuffleArray = (myArray) => {
let randomNum = 0
let resortedArray = []
let unsortedArray = []
for (i=0; i<myArray.length;i++) {
unsortedArray.push(myArray[i])
}
const numOfPeople = unsortedArray.length
for (i=0; i<numOfPeople; i++) {
randomNum = Math.floor(Math.random() * (numOfPeople - i))
resortedArray.push(myArray[randomNum])
myArray.splice(randomNum,1)
}
/* If you want to give shuffleArray a variable please
change where it says nameOfArray to the name of your
array variable.
namesOfArray = unsortedArray
*/
return [unsortedArray,resortedArray]
}
const assignSecretSanta = (names) => {
possSecretSanta = shuffleArray(namesOfParticipants)
while (true) {
possSecretSanta = shuffleArray(possSecretSanta[0])
}
}
const printSecretSanta = (secretSantaPeople) => {
return "Fill in this gap"
}
console.log(printSecretSanta(['Sam', 'Dan', 'Stuart', 'Chris K', 'Rudolph', 'Cliff']))