Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function galacticElections(objArr) {
  2.     let arr = [];
  3.     let galaxy = new Map();
  4.     let votes = new Map();
  5.     let startsWin = new Map();
  6.     for (let i = 0; i < objArr.length; i++) {
  7.         let tempArr = [];
  8.         for (let key in objArr[i]) {
  9.             tempArr.push(objArr[i][key]);
  10.         }
  11.         arr.push(tempArr);
  12.     }
  13.     for (let i = 0; i < arr.length; i++) {
  14.         if (!galaxy.has(arr[i][0])) {
  15.             galaxy.set(arr[i][0], new Map());
  16.         }
  17.         if (!galaxy.get(arr[i][0]).has(arr[i][1])) {
  18.             galaxy.get(arr[i][0]).set(arr[i][1], arr[i][2]);
  19.         } else {
  20.             let oldVot = galaxy.get(arr[i][0]).get(arr[i][1]);
  21.             galaxy.get(arr[i][0]).set(arr[i][1], oldVot + arr[i][2]);
  22.         }
  23.     }
  24.    
  25.    
  26.     for (let i = 0; i < [...galaxy].length; i++) {
  27.         let tu = [...[...[...galaxy][i]][1]].sort((a, b) => a[1] - b[1]).pop();
  28.         let vot = [...[...[...galaxy][i]][1]].reduce((a, b) => a[1] + b[1]);
  29.         if (!startsWin.get([...[...[...galaxy][i]]][0])) {
  30.             startsWin.set([...[...[...galaxy][i]]][0], new Map())
  31.         }
  32.         if (!startsWin.get([...[...[...galaxy][i]]][0]).get(tu[0])) {
  33.             if (vot[1]) {
  34.                 startsWin.get([...[...[...galaxy][i]]][0]).set(tu[0], vot[1]);
  35.             } else {
  36.                 startsWin.get([...[...[...galaxy][i]]][0]).set(tu[0], vot);
  37.             }
  38.         }
  39.     }
  40.     for (let i = 0; i < [...galaxy].length; i++) {
  41.         let tu = [...[...[...galaxy][i]][1]].sort((a, b) => a[1] - b[1]).pop();
  42.         for (let ind of [...[...[...galaxy][i]][1]]) {
  43.             if (!votes.has(tu[0])) {
  44.                 votes.set(tu[0], ind[1]);
  45.             } else {
  46.                 oldV = votes.get(tu[0]);
  47.                 votes.set(tu[0], oldV + ind[1]);
  48.             }
  49.         }
  50.     }
  51.     let allVotes = 0;
  52.     for (let i = 0; i < [...votes].length; i++) {
  53.         allVotes += [...votes][i][1];
  54.     }
  55.     if (votes.length == 1) {
  56.         [...votes] = [...votes].sort((a, b) => b[1] - a[1]);
  57.         console.log(`${votes[0][0]} wins with ${votes[0][1]} votes`);
  58.         console.log(`${votes[0][0]} wins unopposed!`);
  59.     } else if (votes.length = 2 && [...votes].sort((a, b) => b[1] - a[1])[0][1] > (allVotes / 2)) {
  60.         [...votes] = [...votes].sort((a, b) => b[1] - a[1]);
  61.         console.log(`${votes[0][0]} wins with ${votes[0][1]} votes`);
  62.         console.log(`Runner up: ${votes[1][0]}`);
  63.         for (star of startsWin) {
  64.             if (star[1].has(votes[1][0])) {
  65.                 console.log(`${star[0]}: ${[...[...star][1]][0][1]}`);
  66.             }
  67.         }
  68.     } else {
  69.         [...votes] = [...votes].sort((a, b) => b[1] - a[1]);
  70.         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)}%`);
  71.     }
  72. }
  73. galacticElections(
  74.     [ { system: 'Tau',     candidate: 'Flying Shrimp', votes: 150 },
  75.         { system: 'Tau',     candidate: 'Space Cow',     votes: 100 },
  76.         { system: 'Theta',   candidate: 'Space Cow',     votes: 10 },
  77.         { system: 'Sigma',   candidate: 'Space Cow',     votes: 200 },
  78.         { system: 'Sigma',   candidate: 'Flying Shrimp', votes: 75 },
  79.         { system: 'Omicron', candidate: 'Flying Shrimp', votes: 50 },
  80.         { system: 'Omicron', candidate: 'Octocat',       votes: 75 } ]
  81.  
  82. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement