Created
March 12, 2015 02:41
-
-
Save rvbsanjose/fe867b751bb305dbc507 to your computer and use it in GitHub Desktop.
POPSUGAR JavaScript
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 loneSurvivor = function () { | |
// create a list of players | |
var players = []; | |
for (var i = 1; i <= 100; i++) { | |
players.push(i); | |
} | |
// how many to step over | |
var step = 0; | |
var iterations = 0; | |
// repeat as long as their are multiple players | |
while (players.length > 1) { | |
// remove this player from the list | |
players.splice(step, 1); | |
iterations++; | |
step += iterations; | |
step %= players.length; | |
} | |
// return the lone survivor | |
// The player is sitting in chair 31 | |
return players[0]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment