Created
December 9, 2013 15:18
-
-
Save hatched/7873759 to your computer and use it in GitHub Desktop.
Javascript solution to the Secret Santa Dart Dare: https://plus.google.com/u/0/118397406534237711570/posts/XjgFRvqtVA4 ""Your Dart program must take a list of people (first name, last name) and return pairs of Secret Santas. A person can't be their own santa, and a person can't be a santa for someone with the same last name.""
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var people = [ | |
'Zoe Washburne', | |
'Hoban Washburne', | |
'Malcolm Reynolds', | |
'Simon Tam', | |
'River Tam', | |
'Buffy Summers', | |
'Dawn Summers' | |
]; | |
var pairs = [], | |
gifted = [], | |
numPeople = people.length, | |
alreadyGifted = false, | |
loopCount = 0, | |
i = 0; | |
function findRecipient(person, index) { | |
for (i = index; i < numPeople; i++) { | |
var person2 = people[i]; | |
alreadyGifted = gifted.some(function(idx) { return idx === i; } ); | |
if (person !== person2 && !alreadyGifted) { | |
if (person.split(' ')[1] !== person2.split(' ')[1]) { | |
pairs.push(person + ' -> ' + person2); | |
gifted.push(i); | |
loopCount = 0; | |
return; | |
} | |
} else if (i == numPeople-1 && loopCount < 1) { | |
loopCount = 1; | |
findRecipient(person, 0); | |
return; | |
} | |
} | |
} | |
people.forEach(findRecipient); | |
console.dir(pairs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment