Skip to content

Instantly share code, notes, and snippets.

@rvbsanjose
Created March 12, 2015 02:41
Show Gist options
  • Save rvbsanjose/fe867b751bb305dbc507 to your computer and use it in GitHub Desktop.
Save rvbsanjose/fe867b751bb305dbc507 to your computer and use it in GitHub Desktop.
POPSUGAR JavaScript
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