Created
June 13, 2017 13:34
-
-
Save Embraser01/26abf74f51c50b5183aacbed60dd857e to your computer and use it in GitHub Desktop.
Find best couples
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
let request = require('request'); | |
const MORE_OR_LESS = 1; | |
const NB_DAYS = 2; | |
function addIfNotExist(participantsAvailable, participants, iDate) { | |
for (let i = 0; i < participants.length; i++) { | |
if (participants[i].preferences[iDate] > 0 | |
&& participantsAvailable.indexOf(participants[i]) === -1 | |
) { | |
participantsAvailable.push(participants[i]); | |
} | |
} | |
} | |
function findResults(options, participants, dateTab) { | |
const participantsAvailable = []; | |
let i; | |
for (i = 0; i < dateTab.length; i++) addIfNotExist(participantsAvailable, participants, dateTab[i]); | |
if (participantsAvailable.length >= participants.length - MORE_OR_LESS) { | |
let msg = "Le "; | |
for (i = 0; i < dateTab.length - 2; i++) msg += options[dateTab[i]].text + ", "; | |
msg += options[dateTab[i++]].text + " et "; | |
msg += options[dateTab[i]].text + ", sont les jours optimaux (avec %d de marge et sur %d jours)"; | |
console.log(msg, MORE_OR_LESS, NB_DAYS); | |
} | |
} | |
function searchForAll(options, participants, iDates, remainingDays) { | |
if (remainingDays === 0) return findResults(options, participants, iDates); | |
let arrayToSend; | |
for (let i = 0; i < options.length; i++) { | |
arrayToSend = [...iDates]; | |
arrayToSend.push(i); | |
searchForAll(options, participants, arrayToSend, remainingDays - 1); | |
} | |
} | |
request('https://beta.doodle.com/api/v2.0/polls/m9r29w79i4is42zi', (error, response, body) => { | |
let data = JSON.parse(body); | |
let options = data.options; | |
let participants = data.participants; | |
searchForAll(options, participants, [], NB_DAYS); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment