Advertisement
YavorGrancharov

Galactic Elections (Exam Task)

Feb 8th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ballots(input) {
  2.     let galaxy = new Map();
  3.     let winners = new Map();
  4.     let winnerTotalVotes = new Map();
  5.     let totalVotes = 0;
  6.     let systems = input.map(system => system.system);
  7.     let votes = input.map(votes => votes.votes);
  8.     for (let system = 0; system < systems.length; system++) {
  9.         let candidate = input.map(candidate => candidate.candidate);
  10.         if (!galaxy.has(systems[system])) {
  11.             galaxy.set(systems[system], new Map());
  12.         }
  13.         if (!galaxy.get(systems[system]).has(candidate[system])) {
  14.             galaxy.get(systems[system]).set(candidate[system], 0);
  15.         }
  16.         galaxy.get(systems[system])
  17.             .set(candidate[system], galaxy.get(systems[system])
  18.                 .get(candidate[system]) + votes[system]);
  19.  
  20.         totalVotes += votes[system];
  21.     }
  22.     //sort candidates of each system by their votes in descending order
  23.     for (let [system, candidates] of galaxy) {
  24.         galaxy.set(system, new Map([...candidates].sort((a,b) => b[1] - a[1])));
  25.     }
  26.     //sum the votes of repeating candidates in repeating systems and put them into new Map
  27.     for (let [system, candidates] of galaxy) {
  28.         let winner = [...candidates.keys()][0];
  29.         if (!winners.has(winner)) {
  30.             winners.set(winner, new Map());
  31.         }
  32.         winners.get(winner).set(system, [...candidates.values()].reduce((a,b) => a + b));
  33.     }
  34.     //sort the winners in each system by votes in descending order
  35.     for (let [winner, systems] of winners) {
  36.         winners.set(winner, new Map([...systems].sort((a,b) => b[1] - a[1])));
  37.     }
  38.     //assign the total votes from system elections to the winner of the system
  39.     for (let [winner, systems] of winners) {
  40.         winnerTotalVotes.set(winner, [...systems.values()].reduce((a,b) => a + b));
  41.     }
  42.     //get winners from each system by total votes in descending order
  43.     winnerTotalVotes = new Map([...winnerTotalVotes].sort((a,b) => b[1] - a[1]));
  44.  
  45.     let winnerName = [...winnerTotalVotes][0][0];
  46.     let winnerVotes = [...winnerTotalVotes][0][1];
  47.     let winnerPercentage = (winnerVotes / totalVotes) * 100;
  48.  
  49.     if ([...winnerTotalVotes].length === 1) {
  50.         console.log(`${winnerName} wins with ${winnerVotes} votes`);
  51.         console.log(`${winnerName} wins unopposed!`);
  52.         return;
  53.     }
  54.  
  55.     let runnerUpName = [...winnerTotalVotes][1][0];
  56.     let runnerUpPercentage = ([...winnerTotalVotes][1][1] / totalVotes) * 100;
  57.  
  58.     if (winnerPercentage > 50) {
  59.         console.log(`${winnerName} wins with ${winnerVotes} votes`);
  60.         console.log(`Runner up: ${runnerUpName}`);
  61.         for (let [system, votes] of winners.get(runnerUpName)) {
  62.             console.log(`${system}: ${votes}`);
  63.         }
  64.         return;
  65.     }
  66.  
  67.     console.log(`Runoff between ${winnerName} with ${Math.floor(winnerPercentage)}% and ${runnerUpName} with ${Math
  68.         .floor(runnerUpPercentage)}%`);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement