Last active
September 18, 2019 19:20
-
-
Save elvisun/efa4461f78a1c093661f0021fa35f3f3 to your computer and use it in GitHub Desktop.
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
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