Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function galacticElections(objArr) {
- let arr = [];
- let galaxy = new Map();
- let votes = new Map();
- let startsWin = new Map();
- for (let i = 0; i < objArr.length; i++) {
- let tempArr = [];
- for (let key in objArr[i]) {
- tempArr.push(objArr[i][key]);
- }
- arr.push(tempArr);
- }
- for (let i = 0; i < arr.length; i++) {
- if (!galaxy.has(arr[i][0])) {
- galaxy.set(arr[i][0], new Map());
- }
- if (!galaxy.get(arr[i][0]).has(arr[i][1])) {
- galaxy.get(arr[i][0]).set(arr[i][1], arr[i][2]);
- } else {
- let oldVot = galaxy.get(arr[i][0]).get(arr[i][1]);
- galaxy.get(arr[i][0]).set(arr[i][1], oldVot + arr[i][2]);
- }
- }
- for (let i = 0; i < [...galaxy].length; i++) {
- let tu = [...[...[...galaxy][i]][1]].sort((a, b) => a[1] - b[1]).pop();
- let vot = [...[...[...galaxy][i]][1]].reduce((a, b) => a[1] + b[1]);
- if (!startsWin.get([...[...[...galaxy][i]]][0])) {
- startsWin.set([...[...[...galaxy][i]]][0], new Map())
- }
- if (!startsWin.get([...[...[...galaxy][i]]][0]).get(tu[0])) {
- if (vot[1]) {
- startsWin.get([...[...[...galaxy][i]]][0]).set(tu[0], vot[1]);
- } else {
- startsWin.get([...[...[...galaxy][i]]][0]).set(tu[0], vot);
- }
- }
- }
- for (let i = 0; i < [...galaxy].length; i++) {
- let tu = [...[...[...galaxy][i]][1]].sort((a, b) => a[1] - b[1]).pop();
- for (let ind of [...[...[...galaxy][i]][1]]) {
- if (!votes.has(tu[0])) {
- votes.set(tu[0], ind[1]);
- } else {
- oldV = votes.get(tu[0]);
- votes.set(tu[0], oldV + ind[1]);
- }
- }
- }
- let allVotes = 0;
- for (let i = 0; i < [...votes].length; i++) {
- allVotes += [...votes][i][1];
- }
- if (votes.length == 1) {
- [...votes] = [...votes].sort((a, b) => b[1] - a[1]);
- console.log(`${votes[0][0]} wins with ${votes[0][1]} votes`);
- console.log(`${votes[0][0]} wins unopposed!`);
- } else if (votes.length = 2 && [...votes].sort((a, b) => b[1] - a[1])[0][1] > (allVotes / 2)) {
- [...votes] = [...votes].sort((a, b) => b[1] - a[1]);
- console.log(`${votes[0][0]} wins with ${votes[0][1]} votes`);
- console.log(`Runner up: ${votes[1][0]}`);
- for (star of startsWin) {
- if (star[1].has(votes[1][0])) {
- console.log(`${star[0]}: ${[...[...star][1]][0][1]}`);
- }
- }
- } else {
- [...votes] = [...votes].sort((a, b) => b[1] - a[1]);
- console.log(`Runoff between ${votes[0][0]} with ${Math.floor((votes[0][1] / allVotes) * 100)}% and ${votes[1][0]} with ${Math.floor((votes[1][1] / allVotes) * 100)}%`);
- }
- }
- galacticElections(
- [ { system: 'Tau', candidate: 'Flying Shrimp', votes: 150 },
- { system: 'Tau', candidate: 'Space Cow', votes: 100 },
- { system: 'Theta', candidate: 'Space Cow', votes: 10 },
- { system: 'Sigma', candidate: 'Space Cow', votes: 200 },
- { system: 'Sigma', candidate: 'Flying Shrimp', votes: 75 },
- { system: 'Omicron', candidate: 'Flying Shrimp', votes: 50 },
- { system: 'Omicron', candidate: 'Octocat', votes: 75 } ]
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement