Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function GalacticVote() {
- let params = arguments[0].filter(x => x != undefined)
- let galaxy = {}
- let winnersByGalaxy = {}
- ReadTheGalaxy(params, galaxy)
- FindTheWinners(galaxy, winnersByGalaxy)
- Printer(winnersByGalaxy)
- function Printer(winnersByGalaxy) {
- //getting all winners in order
- let winnersByName = Object.keys(winnersByGalaxy).sort((a, b) => {
- return winnersByGalaxy[b].totalVotes - winnersByGalaxy[a].totalVotes
- })
- //getting all the votes in the galaxy
- let totalVotesInEllection = 0
- let galaxyKeys = Object.keys(galaxy)
- for (let system of galaxyKeys) {
- totalVotesInEllection += galaxy[system].votes
- }
- // getting the votes of the winner
- let totalVotesOfFirst = 0
- winnersByGalaxy[winnersByName[0]].states.map(x => {
- totalVotesOfFirst += galaxy[x].votes
- })
- // printing the case with only one candidate
- if (winnersByName.length == 1) {
- console.log(winnersByName[0] + " wins with " + totalVotesOfFirst + " votes")
- console.log(winnersByName[0] + " wins unopposed!")
- }
- // printing the case with more than one candidate
- if (winnersByName.length > 1) {
- //getting the votes of the second candidate
- let totalVotesOfSecond = 0
- winnersByGalaxy[winnersByName[1]].states.map(x => {
- totalVotesOfSecond += galaxy[x].votes
- })
- // printing the case with a Runoff
- if (totalVotesInEllection / 2 > totalVotesOfFirst) {
- console.log("Runoff between " + winnersByName[0] + " with " + Math.floor(totalVotesOfFirst / totalVotesInEllection * 100) + "% and " +
- winnersByName[1] + " with " + Math.floor(totalVotesOfSecond / totalVotesInEllection * 100) + "%")
- } else {
- // printing the case with a winner with 50%+
- console.log(winnersByName[0] + " wins with " + totalVotesOfFirst + " votes")
- console.log("Runner up: " + winnersByName[1])
- let sortedStates = winnersByGalaxy[winnersByName[1]].states.sort((a, b) => {
- return galaxy[b].votes - galaxy[a].votes
- })
- for (let system of sortedStates) {
- console.log(system + ": " + galaxy[system].votes)
- }
- }
- }
- }
- // Find all winners in the galaxy
- function FindTheWinners(galaxy, winnersByGalaxy) {
- let tempSystemKeys = Object.keys(galaxy)
- for (let currentSys of tempSystemKeys) {
- let tempContenderKeys = Object.keys(galaxy[currentSys])
- // candidates sorted by vote
- let sortedContenders = tempContenderKeys.sort((a, b) => {
- return galaxy[currentSys][b].votes - galaxy[currentSys][a].votes
- })
- // Case with only one contender
- if (tempContenderKeys.length == 2) {
- if (!winnersByGalaxy.hasOwnProperty(tempContenderKeys[1])) {
- winnersByGalaxy[tempContenderKeys[1]] = {}
- winnersByGalaxy[tempContenderKeys[1]].states = []
- winnersByGalaxy[tempContenderKeys[1]].states.push(currentSys)
- winnersByGalaxy[tempContenderKeys[1]].totalVotes = galaxy[currentSys].votes
- } else {
- winnersByGalaxy[tempContenderKeys[1]].states.push(currentSys)
- winnersByGalaxy[tempContenderKeys[1]].totalVotes += galaxy[currentSys].votes
- }
- }
- // Case with more than one contenders
- if (tempContenderKeys.length > 2) {
- if (!winnersByGalaxy.hasOwnProperty(tempContenderKeys[1])) {
- winnersByGalaxy[tempContenderKeys[1]] = {}
- winnersByGalaxy[tempContenderKeys[1]].states = []
- winnersByGalaxy[tempContenderKeys[1]].states.push(currentSys)
- winnersByGalaxy[tempContenderKeys[1]].totalVotes = galaxy[currentSys].votes
- } else {
- winnersByGalaxy[tempContenderKeys[1]].states.push(currentSys)
- winnersByGalaxy[tempContenderKeys[1]].totalVotes += galaxy[currentSys].votes
- }
- }
- }
- }
- // Read The Galaxy
- function ReadTheGalaxy(params, galaxy) {
- //Creating unique galaxys
- while (params.length != 0) {
- let currentSys = params.pop()
- if (!galaxy.hasOwnProperty(currentSys.system)) {
- galaxy[currentSys.system] = {}
- galaxy[currentSys.system].votes = 0
- }
- //Creating unique candidates
- if (!galaxy[currentSys.system].hasOwnProperty(currentSys.candidate)) {
- galaxy[currentSys.system][currentSys.candidate] = {}
- galaxy[currentSys.system][currentSys.candidate].votes = currentSys.votes
- galaxy[currentSys.system].votes += currentSys.votes
- } else {
- galaxy[currentSys.system][currentSys.candidate].votes += currentSys.votes
- galaxy[currentSys.system].votes += currentSys.votes
- }
- }
- }
- }
- GalacticVote(
- [ { 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