Advertisement
xapu

Untitled

Jun 19th, 2017
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function GalacticVote() {
  2.     let params = arguments[0].filter(x => x != undefined)
  3.  
  4.     let galaxy = {}
  5.     let winnersByGalaxy = {}
  6.  
  7.     ReadTheGalaxy(params, galaxy)
  8.     FindTheWinners(galaxy, winnersByGalaxy)
  9.     Printer(winnersByGalaxy)
  10.  
  11.     function Printer(winnersByGalaxy) {
  12.        
  13.         //getting all winners in order
  14.         let winnersByName = Object.keys(winnersByGalaxy).sort((a, b) => {
  15.             return winnersByGalaxy[b].totalVotes - winnersByGalaxy[a].totalVotes
  16.         })
  17.  
  18.  
  19.         //getting all the votes in the galaxy
  20.         let totalVotesInEllection = 0
  21.         let galaxyKeys = Object.keys(galaxy)
  22.         for (let system of galaxyKeys) {
  23.             totalVotesInEllection += galaxy[system].votes
  24.         }
  25.  
  26.         // getting the votes of the winner
  27.         let totalVotesOfFirst = 0
  28.         winnersByGalaxy[winnersByName[0]].states.map(x => {
  29.             totalVotesOfFirst += galaxy[x].votes
  30.         })
  31.  
  32.         // printing the case with only one candidate
  33.         if (winnersByName.length == 1) {
  34.             console.log(winnersByName[0] + " wins with " + totalVotesOfFirst + " votes")
  35.             console.log(winnersByName[0] + " wins unopposed!")
  36.         }
  37.  
  38.  
  39.         // printing the case with more than one candidate
  40.         if (winnersByName.length > 1) {
  41.  
  42.             //getting the votes of the second candidate
  43.             let totalVotesOfSecond = 0
  44.             winnersByGalaxy[winnersByName[1]].states.map(x => {
  45.                 totalVotesOfSecond += galaxy[x].votes
  46.             })
  47.  
  48.             // printing the case with a Runoff
  49.             if (totalVotesInEllection / 2 > totalVotesOfFirst) {
  50.                 console.log("Runoff between " + winnersByName[0] + " with " + Math.floor(totalVotesOfFirst / totalVotesInEllection * 100) + "% and " +
  51.                     winnersByName[1] + " with " + Math.floor(totalVotesOfSecond / totalVotesInEllection * 100) + "%")
  52.             } else {
  53.                 // printing the case with a winner with 50%+
  54.                 console.log(winnersByName[0] + " wins with " + totalVotesOfFirst + " votes")
  55.                 console.log("Runner up: " + winnersByName[1])
  56.  
  57.                 let sortedStates = winnersByGalaxy[winnersByName[1]].states.sort((a, b) => {
  58.                     return galaxy[b].votes - galaxy[a].votes
  59.                 })
  60.                 for (let system of sortedStates) {
  61.                     console.log(system + ": " + galaxy[system].votes)
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.     // Find all winners in the galaxy
  68.     function FindTheWinners(galaxy, winnersByGalaxy) {
  69.  
  70.         let tempSystemKeys = Object.keys(galaxy)
  71.  
  72.         for (let currentSys of tempSystemKeys) {
  73.  
  74.             let tempContenderKeys = Object.keys(galaxy[currentSys])
  75.  
  76.             // candidates sorted by vote
  77.  
  78.             let sortedContenders = tempContenderKeys.sort((a, b) => {
  79.                 return galaxy[currentSys][b].votes - galaxy[currentSys][a].votes
  80.             })
  81.  
  82.             // Case with only one contender
  83.             if (tempContenderKeys.length == 2) {
  84.  
  85.                 if (!winnersByGalaxy.hasOwnProperty(tempContenderKeys[1])) {
  86.                     winnersByGalaxy[tempContenderKeys[1]] = {}
  87.                     winnersByGalaxy[tempContenderKeys[1]].states = []
  88.                     winnersByGalaxy[tempContenderKeys[1]].states.push(currentSys)
  89.                     winnersByGalaxy[tempContenderKeys[1]].totalVotes = galaxy[currentSys].votes
  90.                 } else {
  91.                     winnersByGalaxy[tempContenderKeys[1]].states.push(currentSys)
  92.                     winnersByGalaxy[tempContenderKeys[1]].totalVotes += galaxy[currentSys].votes
  93.                 }
  94.             }
  95.  
  96.             // Case with more than one contenders
  97.             if (tempContenderKeys.length > 2) {
  98.                 if (!winnersByGalaxy.hasOwnProperty(tempContenderKeys[1])) {
  99.                     winnersByGalaxy[tempContenderKeys[1]] = {}
  100.                     winnersByGalaxy[tempContenderKeys[1]].states = []
  101.                     winnersByGalaxy[tempContenderKeys[1]].states.push(currentSys)
  102.                     winnersByGalaxy[tempContenderKeys[1]].totalVotes = galaxy[currentSys].votes
  103.                 } else {
  104.                     winnersByGalaxy[tempContenderKeys[1]].states.push(currentSys)
  105.                     winnersByGalaxy[tempContenderKeys[1]].totalVotes += galaxy[currentSys].votes
  106.                 }
  107.             }
  108.  
  109.         }
  110.     }
  111.  
  112.     // Read The Galaxy
  113.     function ReadTheGalaxy(params, galaxy) {
  114.  
  115.         //Creating unique galaxys
  116.  
  117.         while (params.length != 0) {
  118.             let currentSys = params.pop()
  119.             if (!galaxy.hasOwnProperty(currentSys.system)) {
  120.                 galaxy[currentSys.system] = {}
  121.                 galaxy[currentSys.system].votes = 0
  122.             }
  123.             //Creating unique candidates
  124.             if (!galaxy[currentSys.system].hasOwnProperty(currentSys.candidate)) {
  125.                 galaxy[currentSys.system][currentSys.candidate] = {}
  126.                 galaxy[currentSys.system][currentSys.candidate].votes = currentSys.votes
  127.                 galaxy[currentSys.system].votes += currentSys.votes
  128.             } else {
  129.                 galaxy[currentSys.system][currentSys.candidate].votes += currentSys.votes
  130.                 galaxy[currentSys.system].votes += currentSys.votes
  131.             }
  132.         }
  133.     }
  134. }
  135.  
  136. GalacticVote(
  137.  [ { system: 'Tau',     candidate: 'Flying Shrimp', votes: 150 },
  138.   { system: 'Tau',     candidate: 'Space Cow',     votes: 100 },
  139.   { system: 'Theta',   candidate: 'Space Cow',     votes: 10 },
  140.   { system: 'Sigma',   candidate: 'Space Cow',     votes: 200 },
  141.   { system: 'Sigma',   candidate: 'Flying Shrimp', votes: 75 },
  142.   { system: 'Omicron', candidate: 'Flying Shrimp', votes: 50 },
  143.   { system: 'Omicron', candidate: 'Octocat',       votes: 75 } ]
  144. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement