Created
November 25, 2018 13:49
-
-
Save Embraser01/21d582e2324afe5845fb0467795e7169 to your computer and use it in GitHub Desktop.
Generate directed pairs for christmas gifts
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
const PEOPLE = [ | |
{ name: 'John Doe', email: 'johndoe@gmail.com' }, | |
{ name: 'Lea' }, | |
{ name: 'Bob' }, | |
]; | |
/** | |
* Return a new list shuffled. | |
* | |
* @param originalList {Array<Object>} | |
* @return {*} | |
*/ | |
function shuffleList(originalList) { | |
const newList = [...originalList]; | |
for (let i = newList.length - 1; i > 0; i--) { | |
const rand = Math.floor(Math.random() * (i + 1)); | |
[newList[i], newList[rand]] = [newList[rand], newList[i]]; | |
} | |
return newList; | |
} | |
/** | |
* Return pairs. | |
* | |
* @param list {Array<Object>} | |
*/ | |
function createDirectedPairs(list) { | |
return list.map((p, i, arr) => [p, arr[i + 1] || arr[0]]); | |
} | |
/** | |
* console.log Pairs | |
* | |
* @param pairs {Array<Object>} | |
*/ | |
function logPairs(pairs) { | |
pairs | |
.map(([src, dest]) => `${src.name} offre à ${dest.name}`) | |
.forEach(str => console.log(str)); | |
} | |
function main() { | |
const shuffled = shuffleList(PEOPLE); | |
const pairs = createDirectedPairs(shuffled); | |
logPairs(pairs); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment