Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- /**
- * Week 2 election project sample solution by Jake
- */
- // Class modelling a member of the population who votes in the election.
- class Voter {
- constructor(name, age, vote) {
- this.name = name;
- this.age = age;
- this.vote = vote;
- }
- static validVote(vote) {
- if (vote.length !== 2) return false;
- return true;
- }
- }
- // Class modelling a candidate in the election. Candidates are also voters (they can vote for themselves, or anyone else).
- // However they have some extra properties; the number of votes they receive and the party they belong to.
- class Candidate extends Voter {
- constructor(name, age, party) {
- super(name, age);
- this.name = name;
- this.age = age;
- this.party = party;
- this.numVotes = 0;
- }
- }
- // The class which models the election event. An election consists of a voting population, a list of candidates and a winning candidate.
- // voters should be an object whose keys are candidate IDs, and values are the Candidate objects themselves.
- class Election {
- constructor(voters, candidates) {
- this.voters = voters;
- this.candidates = candidates;
- this.winner = null;
- }
- // After an election has been run, get the winner and store in this.winner.
- // This method is private and shouldn't be accessed outside the class.
- _getWinner() {
- let highestNumVotes = 0;
- let currentCandidate = null;
- Object.keys(this.candidates).forEach(candidateId => {
- let numVotes = this.candidates[candidateId].numVotes;
- if (highestNumVotes < numVotes) {
- highestNumVotes = numVotes;
- currentCandidate = this.candidates[candidateId];
- }
- });
- this.winner = currentCandidate;
- }
- // 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
- // 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.
- runElection() {
- this.voters.forEach(voter => {
- let vote = voter.vote;
- let weight = 1;
- vote.forEach(candidateId => {
- this.candidates[candidateId].numVotes += weight;
- weight /= 2;
- });
- });
- this._getWinner();
- }
- }
- // A sample population of a small number of voters, stored as an array
- let votingPopulation = [
- new Voter('Jane Finnegan', 19, [1,3]),
- new Voter('Norman Beracha', 35, [3,4]),
- new Voter('Salome Kadek', 22, [2,1,3]),
- new Voter('Wei Li', 19, [1,2]),
- new Voter('Sam MacKinnon', 59, [1,4])
- ];
- // The election candidates, stored as an object where each object key is the candidate ID, and the object
- // value is the candidate object itself.
- let candidates = {
- 1: new Candidate('Tamara Faiza', 46, 'Pizza Party'),
- 2: new Candidate('Aylin Duke', 39, 'Foam Party'),
- 3: new Candidate('Clay Roderick', 54, 'Flat Earth Party'),
- 4: new Candidate('Nour al-Din', 32, 'Pizza Party'),
- };
- let validVoters = votingPopulation.filter(voter => Voter.validVote(voter.vote));
- let election = new Election(validVoters, candidates);
- election.runElection();
- let winner = election.winner;
- console.log(winner.name + " has won with " + winner.numVotes + " votes.");
- </script>
Add Comment
Please, Sign In to add comment