Last active
March 10, 2020 21:03
-
-
Save buritica/4acff0aade8964440043e6984d8b637c to your computer and use it in GitHub Desktop.
his script is the raffle for 20 scholarships to my Engineering Management course based on this tweet https://twitter.com/buritica/status/1235268210863345664 */
This file contains hidden or 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
/** | |
* This script is the raffle for 20 scholarships to my Engineering Management course | |
* based on this tweet https://twitter.com/buritica/status/1235268210863345664 | |
*/ | |
let participants = [] | |
// get all the replies to the tweet | |
let questions = document.querySelectorAll('article') | |
/** | |
* This function evaluates if there is a twitter user in the text | |
* and it adds it to the list of participants as long as it's not | |
* buritica, platzi or celis | |
* @param {[type]} span [description] | |
*/ | |
function addToRaffle (span) { | |
let participant = span.innerText | |
if (participant === '@buritica' || participant === '@platzi' || participant === '@CelisMX') { | |
return false | |
} | |
if (participant.indexOf('@') === 0) { | |
participants.push(participant) | |
} | |
} | |
/** | |
* check if the value is unique in an array | |
*/ | |
function onlyUnique (value, index, self) { | |
return self.indexOf(value) === index | |
} | |
/** | |
* Shuffle an array from | |
* https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array | |
*/ | |
function shuffle (array) { | |
let currentIndex = array.length, temporaryValue, randomIndex | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
// And swap it with the current element. | |
temporaryValue = array[currentIndex]; | |
array[currentIndex] = array[randomIndex]; | |
array[randomIndex] = temporaryValue; | |
} | |
return array; | |
} | |
console.log('procesando %s preguntas', questions.length) | |
// we iterate over the list of questions and look for all the spans | |
// because twitter html is unintelligible | |
// and then we take those spans and try to find users to add to the raffle | |
questions.forEach(question => { | |
let spans = question.querySelectorAll('span') | |
spans.forEach(addToRaffle) | |
}) | |
console.log('tenemos %s usuarios', participants.length) | |
// we now make sure that the list of raffle participants has no duplicates | |
participants = participants.filter(onlyUnique) | |
console.log('tenemos %s usuarios unicos', participants.length) | |
// we shuffle the list of participants a couple times to make the raffle fair | |
participants = shuffle(participants) | |
participants = shuffle(participants) | |
participants = shuffle(participants) | |
// we pick 20 users | |
participants.length = 20 | |
// we write the 20 lucky winners | |
console.log('los %s ganadores son %s', participants.length, participants.join(', ')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment