diff --git a/election-part2.js b/election-part2.js index 464babb..7eb3196 100644 --- a/election-part2.js +++ b/election-part2.js @@ -1,36 +1,139 @@ -// Importing the functions from what you did in part 1. -const { - candidatesObjToArray, - filterInvalidVoters, - runElection, - getWinner, - winnerMessage, -} = require('./election'); +// // Importing the functions from what you did in part 1. +// const { +// candidatesObjToArray, +// filterInvalidVoters, +// runElection, +// getWinner, +// winnerMessage, +// } = require('./election'); +function candidatesObjToArray(candidates) { + var arrayCandidates = Object.keys(candidates).map(function (key) { // first method + return candidates[key]; + }); + // var arrayCandidates = Object.values(candidates); // second method + return arrayCandidates; +}; + +function filterInvalidVoters(allVoters) { + var arrayVoters = allVoters.filter(function (item) { + if (item.votingCard.length < 3 && item.votingCard[0] !== item.votingCard[1]) { + return item; + }; + }); + return arrayVoters; +} + +function runElection(voters, candidates) { + for (var i = 0; i < voters.length; i++) { + for (var j = 1; j <= Object.keys(candidates).length; j++) { + if (voters[i].votingCard[0] === j) + candidates[j].numVotes += 1; + if (voters[i].votingCard[1] === j) + candidates[j].numVotes += 0.5; + }; + }; + return candidates; +}; + +function getWinner(candidates) { + var winVotes = 0; + var notWin = 0; + var winner = {}; + Object.values(candidates).forEach(function (item) { + if (item.numVotes >= winVotes) { + notWin = winVotes; + winVotes = item.numVotes; + winner = item; + }; + }); + if (notWin === winVotes) { + return null + } else { + return winner + }; +}; + +function winnerMessage(winner) { + var winner = getWinner(candidates) + if (winner !== null) { + var message = winner.name + " has won the election with " + winner.numVotes + " votes!"; + return message + } else { + return "The election was a draw" + }; +}; + +function createList(container, array) { // Function to create list of Voters and Candidates + var ul = document.createElement('ul'); + for (i = 0; i < array.length; i++) { + var li = document.createElement("li"); + var textNode = document.createTextNode(array[i].name); + li.appendChild(textNode); + ul.appendChild(li); + console.log(li); + }; + container[0].appendChild(ul); +}; /** * 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 Candidate extends Voter { + constructor(name, age, votingCard, party, numVotes) { + super(name, age, 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]) +]; +// console.log(votingPopulation); // 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)); @@ -39,5 +142,21 @@ let validVoters = filterInvalidVoters(allVoters); let election = new Election(validVoters, candidates); election.runElection(); // Example of how runElection() can be called. - +// Create List of Voters +var container = document.getElementsByClassName('voters'); +createList(container, votingPopulation); +//*********************** +// Create list of Candidates +var container = document.getElementsByClassName('candidates'); +createList(container, candidatesObjToArray(candidates)); +//********************** +// Create Button Vote!!! +var button = document.createElement('button'); +button.setAttribute('id', 'run-election-btn'); +var text = document.createTextNode('Run Election'); +button.appendChild(text); +button.addEventListener('click', runElection); +var parentBut = document.getElementsByClassName('button-vote') +parentBut[0].appendChild(button); +// ************************************** console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. diff --git a/election.js b/election.js index 776f3a1..f9e7963 100644 --- a/election.js +++ b/election.js @@ -6,23 +6,41 @@ * 1 - Convert candidates object to array */ function candidatesObjToArray(candidates) { - -} + var arrayCandidates = Object.keys(candidates).map(function (key) { // first method + return candidates[key]; + }); + // var arrayCandidates = Object.values(candidates); // second method + return arrayCandidates; +}; /** * 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) { + var arrayVoters = allVoters.filter(function (item) { + if (item.votingCard.length < 3 && item.votingCard[0] !== item.votingCard[1]) { + return item; + }; + }); + return arrayVoters; } /** * 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) { -} +function runElection(voters, candidates) { + for (var i = 0; i < voters.length; i++) { + for (var j = 1; j <= Object.keys(candidates).length; j++) { + if (voters[i].votingCard[0] === j) + candidates[j].numVotes += 1; + if (voters[i].votingCard[1] === j) + candidates[j].numVotes += 0.5; + }; + }; + return candidates; +}; /** * 4 - After an election has been run, return the winner @@ -30,33 +48,53 @@ function runElection(voters, candidates) { * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} */ function getWinner(candidates) { - -} + var winVotes = 0; + var notWin = 0; + var winner = {}; + Object.values(candidates).forEach(function (item) { + if (item.numVotes >= winVotes) { + notWin = winVotes; + winVotes = item.numVotes; + winner = item; + }; + }); + if (notWin === winVotes) { + 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) { - -} + var winner = getWinner(candidates) + if (winner !== null) { + var message = winner.name + " has won the election with " + winner.numVotes + " votes!"; + return message + } else { + return "The election was a draw" + }; +}; // A sample population of a small number of voters, stored as an array let votingPopulation = [ - {name: 'Jane Finnegan', age: 19, votingCard: [1,3]}, - {name: 'Norman Beracha', age: 35, votingCard: [3,4]}, - {name: 'Salome Kadek', age: 22, votingCard: [2,1,3]}, - {name: 'Wei Li', age: 19, votingCard: [1,2]}, - {name: 'Sam MacKinnon', age: 59, votingCard: [1,4]} + { name: 'Jane Finnegan', age: 19, votingCard: [1, 3] }, + { name: 'Norman Beracha', age: 35, votingCard: [3, 4] }, + { name: 'Salome Kadek', age: 22, votingCard: [2, 1, 3] }, + { name: 'Wei Li', age: 19, votingCard: [1, 2] }, + { name: 'Sam MacKinnon', age: 59, votingCard: [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: {name: 'Tamara Faiza', age: 46, votingCard: [1,1], party: 'Pizza Party', numVotes: 0}, - 2: {name: 'Aylin Duke', age: 39, votingCard: [2,2], party: 'Foam Party', numVotes: 0}, - 3: {name: 'Clay Roderick', age: 54, votingCard: [3,4], party: 'Flat Earth Party', numVotes: 0}, - 4: {name: 'Nour al-Din', age: 32, votingCard: [4,1], party: 'Pizza Party', numVotes: 0} + 1: { name: 'Tamara Faiza', age: 46, votingCard: [1, 1], party: 'Pizza Party', numVotes: 0 }, + 2: { name: 'Aylin Duke', age: 39, votingCard: [2, 2], party: 'Foam Party', numVotes: 0 }, + 3: { name: 'Clay Roderick', age: 54, votingCard: [3, 4], party: 'Flat Earth Party', numVotes: 0 }, + 4: { name: 'Nour al-Din', age: 32, votingCard: [4, 1], party: 'Pizza Party', numVotes: 0 } }; let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); @@ -68,10 +106,10 @@ candidates = runElection(validVoters, candidates); let winner = getWinner(candidates); module.exports = { - candidatesObjToArray, - filterInvalidVoters, - runElection, - getWinner, - winnerMessage + candidatesObjToArray, + filterInvalidVoters, + runElection, + getWinner, + winnerMessage } diff --git a/index.html b/index.html index 90b9358..4fd4d67 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,39 @@ + + - + + + CYF-Election + + +
+
+

CYF-Election:

+
+
+
+ +
+
+ +
+
+ +
+
+ + + +
+ + \ No newline at end of file