From 2dec6b41ef83fd4bdd1e4db97ed06e8e63f6b8c4 Mon Sep 17 00:00:00 2001 From: vikrep Date: Wed, 7 Mar 2018 19:14:48 +0000 Subject: [PATCH 01/11] exersise 1 --- election.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/election.js b/election.js index 776f3a1..1fd1659 100644 --- a/election.js +++ b/election.js @@ -6,8 +6,12 @@ * 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. @@ -43,20 +47,20 @@ function winnerMessage(winner) { // 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 +72,10 @@ candidates = runElection(validVoters, candidates); let winner = getWinner(candidates); module.exports = { - candidatesObjToArray, - filterInvalidVoters, - runElection, - getWinner, - winnerMessage + candidatesObjToArray, + filterInvalidVoters, + runElection, + getWinner, + winnerMessage } From a2ff666c486c0620cfdf213e475e1bf1554acc5b Mon Sep 17 00:00:00 2001 From: vikrep Date: Wed, 7 Mar 2018 19:33:14 +0000 Subject: [PATCH 02/11] exersise 2 --- election.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/election.js b/election.js index 1fd1659..35d65a1 100644 --- a/election.js +++ b/election.js @@ -16,8 +16,13 @@ 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) { + var arrayVoters = allVoters.filter(function (item) { + if (item.votingCard.length < 3 && item.votingCard[0] !== item.votingCard[1]) { + return item; + }; + }); + return arrayVoters; } /** From b88b3345bdd589d221f8df77ab31e43b3e586a11 Mon Sep 17 00:00:00 2001 From: vikrep Date: Thu, 8 Mar 2018 11:35:27 +0000 Subject: [PATCH 03/11] exercise 3 --- election.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/election.js b/election.js index 35d65a1..abddcf2 100644 --- a/election.js +++ b/election.js @@ -29,9 +29,18 @@ function filterInvalidVoters(allVoters) { * 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 From d054c4aa9137cd50a5988fcbfec486ec7e854858 Mon Sep 17 00:00:00 2001 From: vikrep Date: Thu, 8 Mar 2018 17:57:32 +0000 Subject: [PATCH 04/11] exercise 4 --- election.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/election.js b/election.js index abddcf2..9b237fc 100644 --- a/election.js +++ b/election.js @@ -48,8 +48,22 @@ 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 From 8bbea8e30e7ce95a9e8fe61ee05cf91f5015e47f Mon Sep 17 00:00:00 2001 From: vikrep Date: Thu, 8 Mar 2018 18:11:55 +0000 Subject: [PATCH 05/11] exercise 5 --- election.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/election.js b/election.js index 9b237fc..58c699c 100644 --- a/election.js +++ b/election.js @@ -70,8 +70,14 @@ function getWinner(candidates) { * 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 = [ From 401ca9510024e8336a332a4855ef0082d3a8cf68 Mon Sep 17 00:00:00 2001 From: vikrep Date: Thu, 8 Mar 2018 18:43:41 +0000 Subject: [PATCH 06/11] some changed --- election.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/election.js b/election.js index 58c699c..f9e7963 100644 --- a/election.js +++ b/election.js @@ -52,7 +52,7 @@ function getWinner(candidates) { var notWin = 0; var winner = {}; Object.values(candidates).forEach(function (item) { - if (item.numVotes > winVotes) { + if (item.numVotes >= winVotes) { notWin = winVotes; winVotes = item.numVotes; winner = item; From 8c44afb3145b4a3026fa5f151ca25b2efce14bfc Mon Sep 17 00:00:00 2001 From: vikrep Date: Sat, 17 Mar 2018 15:12:41 +0000 Subject: [PATCH 07/11] part 2 Q1-Q2 --- election-part2.js | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/election-part2.js b/election-part2.js index 464babb..04187e1 100644 --- a/election-part2.js +++ b/election-part2.js @@ -10,13 +10,26 @@ 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 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. @@ -25,12 +38,22 @@ const { // 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') +}; +console.log(candidates); let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); From a9869a75c21dc906bd41927e280eac1f1b4d7c51 Mon Sep 17 00:00:00 2001 From: vikrep Date: Tue, 20 Mar 2018 20:38:41 +0000 Subject: [PATCH 08/11] part 2 Q3 --- election-part2.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/election-part2.js b/election-part2.js index 04187e1..49513d3 100644 --- a/election-part2.js +++ b/election-part2.js @@ -34,7 +34,22 @@ class Candidate extends Voter { /** * 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. @@ -53,7 +68,7 @@ let candidates = { 3: new Candidate('Clay Roderick', 54, [3, 4], 'Flat Earth Party'), 4: new Candidate('Nour al-Din', 32, [4, 1], 'Pizza Party') }; -console.log(candidates); + let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); From 227aa5db72e302d7b9487cc6ce0970e7faf5f792 Mon Sep 17 00:00:00 2001 From: vikrep Date: Wed, 21 Mar 2018 23:18:32 +0000 Subject: [PATCH 09/11] part 2 Q4 --- election-part2.js | 22 ++++++++++++++++++---- index.html | 11 ++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/election-part2.js b/election-part2.js index 49513d3..2dbe60e 100644 --- a/election-part2.js +++ b/election-part2.js @@ -1,4 +1,4 @@ -// Importing the functions from what you did in part 1. +// // Importing the functions from what you did in part 1. const { candidatesObjToArray, filterInvalidVoters, @@ -18,7 +18,6 @@ class Voter { }; }; - /** * 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. @@ -60,7 +59,7 @@ let votingPopulation = [ new Voter('Wei Li', 19, [1, 2]), new Voter('Sam MacKinnon', 59, [1, 4]) ]; - +// console.log(votingPopulation); // Include your candidates object here. let candidates = { 1: new Candidate('Tamara Faiza', 46, [1, 1], 'Pizza Party'), @@ -68,7 +67,7 @@ let candidates = { 3: new Candidate('Clay Roderick', 54, [3, 4], 'Flat Earth Party'), 4: new Candidate('Nour al-Din', 32, [4, 1], 'Pizza Party') }; - +// console.log(candidates); let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); @@ -78,4 +77,19 @@ let election = new Election(validVoters, candidates); election.runElection(); // Example of how runElection() can be called. + +function createList(array) { // Function to create list of Voter + 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); + }; + document.body.appendChild(ul); +}; + +createList(votingPopulation); + console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. diff --git a/index.html b/index.html index 90b9358..918f363 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,16 @@ + + - + + + CYF-Election + + + + + \ No newline at end of file From 0990bee590684c97b6b2ca354c1cf360bb7a9d9b Mon Sep 17 00:00:00 2001 From: vikrep Date: Thu, 22 Mar 2018 19:12:45 +0000 Subject: [PATCH 10/11] work with index.html --- election-part2.js | 102 +++++++++++++++++++++++++++++++++++++--------- index.html | 27 +++++++++++- 2 files changed, 108 insertions(+), 21 deletions(-) diff --git a/election-part2.js b/election-part2.js index 2dbe60e..debe1ad 100644 --- a/election-part2.js +++ b/election-part2.js @@ -1,11 +1,79 @@ // // Importing the functions from what you did in part 1. -const { - candidatesObjToArray, - filterInvalidVoters, - runElection, - getWinner, - winnerMessage, -} = require('./election'); +// 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(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); + }; + document.body.appendChild(ul); +}; /** * 1 - Write a Voter class modelling a member of the population who votes in the election. @@ -77,19 +145,15 @@ let election = new Election(validVoters, candidates); election.runElection(); // Example of how runElection() can be called. +createList(votingPopulation); -function createList(array) { // Function to create list of Voter - 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); - }; - document.body.appendChild(ul); -}; +createList(candidatesObjToArray(candidates)); -createList(votingPopulation); +var button = document.createElement('button'); +button.setAttribute('id', 'run-election-btn'); +var text = document.createTextNode('Run Election'); +button.appendChild(text); +button.addEventListener('click', runElection); +document.body.appendChild(button); console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. diff --git a/index.html b/index.html index 918f363..3fc29aa 100644 --- a/index.html +++ b/index.html @@ -5,12 +5,35 @@ CYF-Election - + + +
+
+

CYF-Election:

+
+
+
+ +
+
+ +
+
+ +
+
+ - + +
\ No newline at end of file From e9368153f49a5d4d8603fb0082dcbb784d6aaf6c Mon Sep 17 00:00:00 2001 From: vikrep Date: Fri, 23 Mar 2018 00:08:39 +0000 Subject: [PATCH 11/11] create DOM --- election-part2.js | 25 ++++++++++++++----------- index.html | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/election-part2.js b/election-part2.js index debe1ad..7eb3196 100644 --- a/election-part2.js +++ b/election-part2.js @@ -63,7 +63,7 @@ function winnerMessage(winner) { }; }; -function createList(array) { // Function to create list of Voters and Candidates +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"); @@ -72,7 +72,7 @@ function createList(array) { // Function to create list of Voters and Can ul.appendChild(li); console.log(li); }; - document.body.appendChild(ul); + container[0].appendChild(ul); }; /** @@ -118,7 +118,6 @@ class Election { }; }; - // Include your votingPopulation array here. let votingPopulation = [ new Voter('Jane Finnegan', 19, [1, 3]), @@ -135,7 +134,6 @@ let candidates = { 3: new Candidate('Clay Roderick', 54, [3, 4], 'Flat Earth Party'), 4: new Candidate('Nour al-Din', 32, [4, 1], 'Pizza Party') }; -// console.log(candidates); let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); @@ -144,16 +142,21 @@ let validVoters = filterInvalidVoters(allVoters); let election = new Election(validVoters, candidates); election.runElection(); // Example of how runElection() can be called. - -createList(votingPopulation); - -createList(candidatesObjToArray(candidates)); - +// 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); -document.body.appendChild(button); - +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/index.html b/index.html index 3fc29aa..4fd4d67 100644 --- a/index.html +++ b/index.html @@ -21,7 +21,7 @@

CYF-Election:

-
+