diff --git a/election-part2.js b/election-part2.js index 464babb..82fd766 100644 --- a/election-part2.js +++ b/election-part2.js @@ -9,28 +9,68 @@ const { /** * 1 - Write a Voter class modelling a member of the population who votes in the election. - */ + */ +class Voter { + constructor(name, age, votingCard) { + this.name = name, + this.age = age, + this.votingCard = votingCard /** * 2 - Write a Candidate 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. */ +class candidates { + constructor(name, age, votingCard, party, numVotes) { + this.name = name, + this.age = age, + this.votingCard = votingCard, + this.party = party, + this.numVotes = 0 /** * 3 - Write an Election class which models the election. + * */ - +class election { + constructor(validVoters, candidates) { + this.validVoters = validVoters; + this.candidates = candidates; + this.winner = ''; + }; + runElection() { + this.candidates = runElection(this.validVoters, this.candidates) + }; + getWinner() { + this.winner = getWinner(this.candidates) + }; + printWinnerMessage() { + return winnerMessage(this.winner) + }; + // Include your votingPopulation array here. -let votingPopulation = []; - +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]) + ]; + // Include your candidates object here. -let candidates = {}; - +let candidates = { + 1: new Candidate('Tamara Faiza', 46, [1, 1], 'Pizza Party'), + 2: new Candidate('Aylin Duke', 39, [2, 2], 'Foam Party'), + 3: new Candidate('Clay Roderick', 54, [3, 4], 'Flat Earth Party'), + 4: new Candidate('Nour al-Din', 32, [4, 1], 'Pizza Party') + }; + + let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); diff --git a/election.js b/election.js index 776f3a1..bc80bdd 100644 --- a/election.js +++ b/election.js @@ -7,22 +7,48 @@ */ function candidatesObjToArray(candidates) { + + const Array = Object.values(candidates) + + return Array +// const Array = Object.keys(candidates).map(i => candidates[i]) another way +// return Array + + } /** * 2 - Remove any voters who have voted for more than 2 people, or have voted for the same person twice. */ -function filterInvalidVoters(voters) { +function filterInvalidVoters(allVoters) { -} + return allVoters.filter(function (element){ + + return element.votingCard.length <3 && element.votingCard[0] !== element.votingCard[1] + + }); + } /** * 3 - Add up all the votes cast by the voting population. Note that for two adjacent votes in the vote array, * the right vote counts for half of the left vote. */ function runElection(voters, candidates) { + for (var i = 0; i < voters.length; i++) { + for (var x = 1; x <= Object.values(candidates).length; x++) { + if (voters[i].votingCard[0] === x) + candidates[x].numVotes += 1; + if (voters[i].votingCard[1] === x) + candidates[x].numVotes += 0.5; + }; + }; + + return candidates; + }; + + + -} /** * 4 - After an election has been run, return the winner @@ -30,14 +56,34 @@ function runElection(voters, candidates) { * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} */ function getWinner(candidates) { - -} - + var max = 0 + var min = 0 + var winner = {} + + for (var x = 1; x <= Object.values(candidates).length; x++) { + + if (candidates[x].numVotes>max){ + min = max; + max = candidates[x].numVotes + winner = candidates[x] + } + } + if (min === max ){ + return null + } else { + return winner + } + } + + /** * 5 - Return a message including the name of the winner, and how many votes * he/she received */ function winnerMessage(winner) { + //'Tamara Faiza has won the election with 3.5 votes!' + var message = winner.name + " has won the election with " + winner.numVotes + " votes!" +return message }