Last active
August 29, 2015 14:10
-
-
Save birkir/065ac234afa3de697c6d to your computer and use it in GitHub Desktop.
Jóladagatal 2/24 - Jólakveðja
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
/** | |
* Circulate santas saying merry christmas to each other | |
* @param int Numer of santas | |
* @return String | |
*/ | |
function santaSaidMerryChristmas(n) { | |
// vars | |
var santas = Array.apply(null, Array(n)).map(function (_, i) { return (i + 1); }), | |
i = 0; | |
// while two santas are available | |
while (santas.length > 1) { | |
// reset cursor if last sant | |
i = (i >= santas.length) ? 0 : i; | |
// splice the next santa out | |
santas.splice(((i + 1) >= santas.length) ? 0 : (i + 1), 1); | |
// move cursor | |
i++ | |
} | |
// pop last santa | |
return 'Santa ' + santas.pop() + ' was left in the circle'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment