Skip to content

Instantly share code, notes, and snippets.

@elvisun
Last active September 18, 2019 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elvisun/efa4461f78a1c093661f0021fa35f3f3 to your computer and use it in GitHub Desktop.
Save elvisun/efa4461f78a1c093661f0021fa35f3f3 to your computer and use it in GitHub Desktop.
function kill(people) {
const survivors = [];
for(let i =0; i < people.length; i++) {
if (i % 2 === 0) { // index start from 0, so odd is even
console.log("a person is killed");
// then do nothing
}
else {
survivors.push(people[i]);
// Or add to new line;
}
}
return survivors;
}
var people = [];
// Make 100 people
for (let i = 1 ; i <= 100; i++) {
people.push(i);
}
console.log(people);
while (people.length > 1) {
people = kill(people);
console.log(people);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment