Guest User

Untitled

a guest
Dec 7th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. <script>
  2. /**
  3. * Week 2 election project sample solution by Jake
  4. */
  5.  
  6. // Class modelling a member of the population who votes in the election.
  7. class Voter {
  8. constructor(name, age, vote) {
  9. this.name = name;
  10. this.age = age;
  11. this.vote = vote;
  12. }
  13.  
  14. static validVote(vote) {
  15. if (vote.length !== 2) return false;
  16. return true;
  17. }
  18. }
  19.  
  20. // Class modelling a candidate in the election. Candidates are also voters (they can vote for themselves, or anyone else).
  21. // However they have some extra properties; the number of votes they receive and the party they belong to.
  22. class Candidate extends Voter {
  23. constructor(name, age, party) {
  24. super(name, age);
  25. this.name = name;
  26. this.age = age;
  27. this.party = party;
  28. this.numVotes = 0;
  29. }
  30. }
  31.  
  32.  
  33. // The class which models the election event. An election consists of a voting population, a list of candidates and a winning candidate.
  34. // voters should be an object whose keys are candidate IDs, and values are the Candidate objects themselves.
  35. class Election {
  36. constructor(voters, candidates) {
  37. this.voters = voters;
  38. this.candidates = candidates;
  39. this.winner = null;
  40. }
  41.  
  42. // After an election has been run, get the winner and store in this.winner.
  43. // This method is private and shouldn't be accessed outside the class.
  44. _getWinner() {
  45. let highestNumVotes = 0;
  46. let currentCandidate = null;
  47.  
  48. Object.keys(this.candidates).forEach(candidateId => {
  49. let numVotes = this.candidates[candidateId].numVotes;
  50. if (highestNumVotes < numVotes) {
  51. highestNumVotes = numVotes;
  52. currentCandidate = this.candidates[candidateId];
  53. }
  54. });
  55. this.winner = currentCandidate;
  56. }
  57.  
  58. // Tally up all the votes from the population of voters stored in this.voters. For each iteration of a voter, a new loop is required
  59. // to iterate over the votes cast by the voter. For two adjacent votes in the array, the right vote has half the weight of the left.
  60. runElection() {
  61. this.voters.forEach(voter => {
  62. let vote = voter.vote;
  63. let weight = 1;
  64.  
  65. vote.forEach(candidateId => {
  66. this.candidates[candidateId].numVotes += weight;
  67. weight /= 2;
  68. });
  69. });
  70. this._getWinner();
  71. }
  72. }
  73.  
  74. // A sample population of a small number of voters, stored as an array
  75. let votingPopulation = [
  76. new Voter('Jane Finnegan', 19, [1,3]),
  77. new Voter('Norman Beracha', 35, [3,4]),
  78. new Voter('Salome Kadek', 22, [2,1,3]),
  79. new Voter('Wei Li', 19, [1,2]),
  80. new Voter('Sam MacKinnon', 59, [1,4])
  81. ];
  82.  
  83. // The election candidates, stored as an object where each object key is the candidate ID, and the object
  84. // value is the candidate object itself.
  85. let candidates = {
  86. 1: new Candidate('Tamara Faiza', 46, 'Pizza Party'),
  87. 2: new Candidate('Aylin Duke', 39, 'Foam Party'),
  88. 3: new Candidate('Clay Roderick', 54, 'Flat Earth Party'),
  89. 4: new Candidate('Nour al-Din', 32, 'Pizza Party'),
  90. };
  91.  
  92. let validVoters = votingPopulation.filter(voter => Voter.validVote(voter.vote));
  93.  
  94. let election = new Election(validVoters, candidates);
  95.  
  96. election.runElection();
  97.  
  98. let winner = election.winner;
  99.  
  100. console.log(winner.name + " has won with " + winner.numVotes + " votes.");
  101. </script>
Add Comment
Please, Sign In to add comment