From 83deacc2eb1eed3da32277bc54e762dae847f3cd Mon Sep 17 00:00:00 2001 From: ksawalme Date: Sat, 24 Mar 2018 00:30:39 +0000 Subject: [PATCH 1/3] First commit --- election.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/election.js b/election.js index 776f3a1..e0b32fc 100644 --- a/election.js +++ b/election.js @@ -7,6 +7,12 @@ */ function candidatesObjToArray(candidates) { + + const Array = Object.keys(candidates).map(i => candidates[i]) + + return Array + + } /** From d3aa788ca13d34d920a6bc2eecc56985d206aec4 Mon Sep 17 00:00:00 2001 From: ksawalme Date: Fri, 6 Apr 2018 01:50:13 +0100 Subject: [PATCH 2/3] My Solution --- election.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/election.js b/election.js index e0b32fc..bc80bdd 100644 --- a/election.js +++ b/election.js @@ -8,9 +8,11 @@ function candidatesObjToArray(candidates) { - const Array = Object.keys(candidates).map(i => candidates[i]) + const Array = Object.values(candidates) return Array +// const Array = Object.keys(candidates).map(i => candidates[i]) another way +// return Array } @@ -18,17 +20,35 @@ function candidatesObjToArray(candidates) { /** * 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 @@ -36,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 } From c0c0ec8e04196e7602a0292be44f90cbf7636b17 Mon Sep 17 00:00:00 2001 From: ksawalme Date: Sat, 7 Apr 2018 00:31:28 +0100 Subject: [PATCH 3/3] part2 --- election-part2.js | 52 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) 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));