From 5edf5f104636132550b8b72ef339d71a3c033fa4 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 20 Feb 2018 13:04:50 +0000 Subject: [PATCH 01/38] first exercise done --- .vscode/settings.json | 2 ++ election.js | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..61679f2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{"editor.minimap.enabled": false +} \ No newline at end of file diff --git a/election.js b/election.js index 776f3a1..d308cde 100644 --- a/election.js +++ b/election.js @@ -6,7 +6,10 @@ * 1 - Convert candidates object to array */ function candidatesObjToArray(candidates) { - + var arrOfKeys = Object.keys(candidates); + var arrOfCandidates = arrOfKeys.map(function (key) { return candidates[key]; }); + //console.log(arrOfCandidates); + return arrOfCandidates; } /** From 38497d06e58345a56ebeb23bbf251961140b270e Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 20 Feb 2018 15:11:37 +0000 Subject: [PATCH 02/38] second exercise done --- election.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/election.js b/election.js index d308cde..88e6f65 100644 --- a/election.js +++ b/election.js @@ -8,15 +8,17 @@ function candidatesObjToArray(candidates) { var arrOfKeys = Object.keys(candidates); var arrOfCandidates = arrOfKeys.map(function (key) { return candidates[key]; }); - //console.log(arrOfCandidates); return arrOfCandidates; } /** * 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 (item) { + return item.votingCard.length < 3 && item.votingCard[0] !== item.votingCard[1] + }) } /** @@ -63,9 +65,10 @@ let candidates = { }; let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); +console.log(allVoters); let validVoters = filterInvalidVoters(allVoters); - +console.log(validVoters) candidates = runElection(validVoters, candidates); let winner = getWinner(candidates); From 4fd3ab152030142539d3b29e1eedc0b6d8ae9df0 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 20 Feb 2018 16:57:13 +0000 Subject: [PATCH 03/38] third exercise done --- election.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/election.js b/election.js index 88e6f65..a33c01a 100644 --- a/election.js +++ b/election.js @@ -25,10 +25,21 @@ 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(validVoters, candidates) { + votes = []; + for (var i = 0; i < validVoters.length; i++) { + votes.push(validVoters[i].votingCard); + } + for (var i = 0; i < votes.length; i++) { //loop through vote array + for (var j = 1; j < 5; j++) { //loop through candidates array + if (votes[i][0] === j) + candidates[j].numVotes += 1; + if (votes[i][1] === j) + candidates[j].numVotes += 0.5 + } + } + return candidates; } - /** * 4 - After an election has been run, return the winner * @@ -65,11 +76,13 @@ let candidates = { }; let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); -console.log(allVoters); +//console.log(allVoters); let validVoters = filterInvalidVoters(allVoters); -console.log(validVoters) +//console.log(validVoters,"validdddddddddd") + candidates = runElection(validVoters, candidates); +//console.log(candidates); let winner = getWinner(candidates); From b1b62f12411c387f1ad1b020e28f1699a11fdbb7 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 20 Feb 2018 18:50:30 +0000 Subject: [PATCH 04/38] fourth exercise done --- election.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/election.js b/election.js index a33c01a..ae6fdc9 100644 --- a/election.js +++ b/election.js @@ -26,10 +26,14 @@ function filterInvalidVoters(allVoters) { * the right vote counts for half of the left vote. */ function runElection(validVoters, candidates) { - votes = []; + /* votes = []; for (var i = 0; i < validVoters.length; i++) { votes.push(validVoters[i].votingCard); - } + } */ + votes = []; + validVoters.forEach(function (item) { + votes.push(item.votingCard) + }) for (var i = 0; i < votes.length; i++) { //loop through vote array for (var j = 1; j < 5; j++) { //loop through candidates array if (votes[i][0] === j) @@ -46,9 +50,19 @@ function runElection(validVoters, candidates) { * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} */ function getWinner(candidates) { + finalVotes = []; + for (var key in candidates) { + finalVotes.push(candidates[key].numVotes); + } + console.log(finalVotes) + var min = Math.min(...finalVotes); + var max = Math.max(...finalVotes); + if (min === max) return null //if all candidates numVotes is equal so we have no winner + else + return candidates[Object.keys(candidates)[finalVotes.indexOf(max)]] + //horrific line above: the candidate connected to max based on key object, needs refactor! } - /** * 5 - Return a message including the name of the winner, and how many votes * he/she received @@ -79,12 +93,13 @@ let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); //console.log(allVoters); let validVoters = filterInvalidVoters(allVoters); -//console.log(validVoters,"validdddddddddd") +console.log(validVoters,"validdddddddddd") candidates = runElection(validVoters, candidates); -//console.log(candidates); +console.log(candidates); let winner = getWinner(candidates); +console.log("and the winner is: ",winner) module.exports = { candidatesObjToArray, From 29531c520ff5220d63c5808f9e399bc3aba6406d Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 20 Feb 2018 19:00:39 +0000 Subject: [PATCH 05/38] fifth exercise done --- election.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/election.js b/election.js index ae6fdc9..1772b7f 100644 --- a/election.js +++ b/election.js @@ -54,21 +54,21 @@ function getWinner(candidates) { for (var key in candidates) { finalVotes.push(candidates[key].numVotes); } - console.log(finalVotes) var min = Math.min(...finalVotes); var max = Math.max(...finalVotes); if (min === max) return null //if all candidates numVotes is equal so we have no winner else return candidates[Object.keys(candidates)[finalVotes.indexOf(max)]] - //horrific line above: the candidate connected to max based on key object, needs refactor! - + //horrific line above: the candidate connected to max based on key object, needs refactor! } /** * 5 - Return a message including the name of the winner, and how many votes * he/she received */ function winnerMessage(winner) { - + var winner = getWinner(candidates) + var mes = "Congratulation to " + winner.name + "who has achieved " + winner.numVotes + " votes"; + return mes; } // A sample population of a small number of voters, stored as an array @@ -101,6 +101,8 @@ console.log(candidates); let winner = getWinner(candidates); console.log("and the winner is: ",winner) +console.log(winnerMessage()); + module.exports = { candidatesObjToArray, filterInvalidVoters, From 020bf76dd28e6dd974925cfcdcd52dbfcb90f716 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 20 Feb 2018 19:17:03 +0000 Subject: [PATCH 06/38] fifth exercise added sth --- election.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/election.js b/election.js index 1772b7f..da4a949 100644 --- a/election.js +++ b/election.js @@ -67,8 +67,11 @@ function getWinner(candidates) { */ function winnerMessage(winner) { var winner = getWinner(candidates) - var mes = "Congratulation to " + winner.name + "who has achieved " + winner.numVotes + " votes"; - return mes; + if (winner !== null) { + var mes = "Congratulation to " + winner.name + "who has achieved " + winner.numVotes + " votes"; + return mes; + } + else console.log("The election was a draw"); } // A sample population of a small number of voters, stored as an array From 18a20f0a454822abb1147b2eb07a2358dbd76425 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 20 Feb 2018 19:57:10 +0000 Subject: [PATCH 07/38] fifth exercise done and code cleaned a bit --- election.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/election.js b/election.js index da4a949..f9d9782 100644 --- a/election.js +++ b/election.js @@ -68,7 +68,7 @@ function getWinner(candidates) { function winnerMessage(winner) { var winner = getWinner(candidates) if (winner !== null) { - var mes = "Congratulation to " + winner.name + "who has achieved " + winner.numVotes + " votes"; + var mes = winner.name + " has won the election with " + winner.numVotes + " votes."; return mes; } else console.log("The election was a draw"); @@ -96,13 +96,13 @@ let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); //console.log(allVoters); let validVoters = filterInvalidVoters(allVoters); -console.log(validVoters,"validdddddddddd") +//console.log(validVoters,"validdddddddddd") candidates = runElection(validVoters, candidates); -console.log(candidates); +//console.log(candidates); let winner = getWinner(candidates); -console.log("and the winner is: ",winner) +//console.log("and the winner is: ",winner) console.log(winnerMessage()); From 4182ca522989c22ea2c8024345198f1718a2a327 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 20 Feb 2018 20:29:52 +0000 Subject: [PATCH 08/38] sixth exercise done and fourth exercise changed totally logically --- election.js | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/election.js b/election.js index f9d9782..4f1364c 100644 --- a/election.js +++ b/election.js @@ -54,12 +54,19 @@ function getWinner(candidates) { for (var key in candidates) { finalVotes.push(candidates[key].numVotes); } - var min = Math.min(...finalVotes); + /* var min = Math.min(...finalVotes); var max = Math.max(...finalVotes); - if (min === max) return null //if all candidates numVotes is equal so we have no winner + if (min === max) return null //if all candidates numVotes is equal so we have no winner */ + var max = Math.max(...finalVotes); + var maxDuplicate = [] + for (var i = 0; i < finalVotes.length; i++) { + if (finalVotes[i] === max); + maxDuplicate.push(finalVotes[i]) + } + if (maxDuplicate.length > 1) return null; else return candidates[Object.keys(candidates)[finalVotes.indexOf(max)]] - //horrific line above: the candidate connected to max based on key object, needs refactor! + //horrific line above:returns the candidate connected to max based on key object, needs refactor! } /** * 5 - Return a message including the name of the winner, and how many votes @@ -71,25 +78,25 @@ function winnerMessage(winner) { var mes = winner.name + " has won the election with " + winner.numVotes + " votes."; return mes; } - else console.log("The election was a draw"); + 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)); @@ -107,10 +114,10 @@ let winner = getWinner(candidates); console.log(winnerMessage()); module.exports = { - candidatesObjToArray, - filterInvalidVoters, - runElection, - getWinner, - winnerMessage + candidatesObjToArray, + filterInvalidVoters, + runElection, + getWinner, + winnerMessage } From 74046907d41b1c717369176cefa94ba969b27577 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Fri, 23 Feb 2018 21:25:38 +0000 Subject: [PATCH 09/38] fourth exercise modified a bit --- election.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/election.js b/election.js index 4f1364c..a1ee289 100644 --- a/election.js +++ b/election.js @@ -60,8 +60,8 @@ function getWinner(candidates) { var max = Math.max(...finalVotes); var maxDuplicate = [] for (var i = 0; i < finalVotes.length; i++) { - if (finalVotes[i] === max); - maxDuplicate.push(finalVotes[i]) + if (finalVotes[i] === max) + maxDuplicate.push(finalVotes[i]); } if (maxDuplicate.length > 1) return null; else @@ -96,7 +96,7 @@ 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 } + 4: { name: 'Nour al-Din', age: 32, votingCard: [4, 3], party: 'Pizza Party', numVotes: 0 } }; let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); @@ -106,7 +106,7 @@ let validVoters = filterInvalidVoters(allVoters); //console.log(validVoters,"validdddddddddd") candidates = runElection(validVoters, candidates); -//console.log(candidates); +console.log(candidates); let winner = getWinner(candidates); //console.log("and the winner is: ",winner) From 0b40a7f719b7175c476f70cbafcc5a69e5835832 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Fri, 23 Feb 2018 21:29:19 +0000 Subject: [PATCH 10/38] fourth exercise modified a bit --- election.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/election.js b/election.js index a1ee289..9f9c95d 100644 --- a/election.js +++ b/election.js @@ -61,12 +61,12 @@ function getWinner(candidates) { var maxDuplicate = [] for (var i = 0; i < finalVotes.length; i++) { if (finalVotes[i] === max) - maxDuplicate.push(finalVotes[i]); + maxDuplicate.push(finalVotes[i]); } if (maxDuplicate.length > 1) return null; else return candidates[Object.keys(candidates)[finalVotes.indexOf(max)]] - //horrific line above:returns the candidate connected to max based on key object, needs refactor! + //horrific line above:returns the candidate connected to max based on key object, needs refactor! } /** * 5 - Return a message including the name of the winner, and how many votes @@ -106,7 +106,7 @@ let validVoters = filterInvalidVoters(allVoters); //console.log(validVoters,"validdddddddddd") candidates = runElection(validVoters, candidates); -console.log(candidates); +//console.log(candidates); let winner = getWinner(candidates); //console.log("and the winner is: ",winner) From fc0af220ae27f3abb435ad8d015bdce9d4734d91 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Fri, 23 Feb 2018 21:32:34 +0000 Subject: [PATCH 11/38] nour aldin vote changed to 4,1 like it was before sixth exercise --- election.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/election.js b/election.js index 9f9c95d..4110498 100644 --- a/election.js +++ b/election.js @@ -96,7 +96,7 @@ 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, 3], party: 'Pizza Party', numVotes: 0 } + 4: { name: 'Nour al-Din', age: 32, votingCard: [4, 1], party: 'Pizza Party', numVotes: 0 } }; let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); From f165f6aa61033d87e818d4956ce015528f3b5ecc Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Fri, 23 Feb 2018 21:36:43 +0000 Subject: [PATCH 12/38] the message for winner edited --- election.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/election.js b/election.js index 4110498..a2d4e17 100644 --- a/election.js +++ b/election.js @@ -75,7 +75,7 @@ function getWinner(candidates) { function winnerMessage(winner) { var winner = getWinner(candidates) if (winner !== null) { - var mes = winner.name + " has won the election with " + winner.numVotes + " votes."; + var mes = winner.name + " has won the election with " + winner.numVotes + " votes!"; return mes; } else return "The election was a draw"; From 9ebd56662cd8f301b513a6497f3de3458f682f55 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Mon, 26 Feb 2018 17:28:20 +0000 Subject: [PATCH 13/38] part2 first exer done --- election-part2.js | 16 ++++++++++++---- election.js | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/election-part2.js b/election-part2.js index 464babb..c6753fa 100644 --- a/election-part2.js +++ b/election-part2.js @@ -10,20 +10,28 @@ 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; + } +} +new new Voter('Jane Finnegan', 19, [1, 3]); +new Voter('Norman Beracha', 35, [3, 4]); +new Voter('Salome Kadek', 22, [2, 1, 3]); +new Vote('Wei Li', 19, [1, 2]); +new Vote('Sam MacKinnon', 59, [1, 4]); /** * 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. */ - /** * 3 - Write an Election class which models the election. */ - - // Include your votingPopulation array here. let votingPopulation = []; diff --git a/election.js b/election.js index a2d4e17..48379d8 100644 --- a/election.js +++ b/election.js @@ -106,7 +106,7 @@ let validVoters = filterInvalidVoters(allVoters); //console.log(validVoters,"validdddddddddd") candidates = runElection(validVoters, candidates); -//console.log(candidates); +console.log(candidates); let winner = getWinner(candidates); //console.log("and the winner is: ",winner) From 3b73e1f229efcb0436f4cfe2bc3fe29c80c34bbc Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Mon, 26 Feb 2018 17:30:55 +0000 Subject: [PATCH 14/38] part2 second exer done --- election-part2.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/election-part2.js b/election-part2.js index c6753fa..c3f6209 100644 --- a/election-part2.js +++ b/election-part2.js @@ -27,7 +27,16 @@ new Vote('Sam MacKinnon', 59, [1, 4]); * 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(party,numVotes) { + this.party = party; + this.numVotes = numVotes; + } +} +new Candidate('Tamara Faiza', 46, 'Pizza Party', [1,1]); +new Candidate('Aylin Duke',39,'Foam Party', [2, 2]); +new Candidate( 'Clay Roderick',54,'Flat Earth Party',[3, 4]); +new Candidate('Nour al-Din',32, 'Pizza Party', [4, 1]); /** * 3 - Write an Election class which models the election. */ From 73d8033aab7e4c7aa524d273d701c13287af1581 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Mon, 26 Feb 2018 19:20:07 +0000 Subject: [PATCH 15/38] part2 second exer done --- election-part2.js | 18 ++++++++++++++++++ election.js | 2 +- index.html | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/election-part2.js b/election-part2.js index c3f6209..47f65af 100644 --- a/election-part2.js +++ b/election-part2.js @@ -40,6 +40,24 @@ new Candidate('Nour al-Din',32, 'Pizza Party', [4, 1]); /** * 3 - Write an Election class which models the election. */ +class Election{ + constructor(validVoters,candidates,winner){ + this.validVoters=validVoters; + this.candidates=candidates; + this.winner=winner; + } + runElection() { + // altering the voting cards of the candidates class variable + + } getWinner() { + //setting the winner class variable after the winner is calculated + + } printWinnerMessage() { + // should print a message as before including who won, + //and how many votes he/she received + + } +} // Include your votingPopulation array here. let votingPopulation = []; diff --git a/election.js b/election.js index 48379d8..9373d44 100644 --- a/election.js +++ b/election.js @@ -66,7 +66,7 @@ function getWinner(candidates) { if (maxDuplicate.length > 1) return null; else return candidates[Object.keys(candidates)[finalVotes.indexOf(max)]] - //horrific line above:returns the candidate connected to max based on key object, needs refactor! + //horrible line above:returns the candidate connected to max based on key object, needs refactor! } /** * 5 - Return a message including the name of the winner, and how many votes diff --git a/index.html b/index.html index 90b9358..e00ca0a 100644 --- a/index.html +++ b/index.html @@ -4,4 +4,5 @@ + From e6d7b1bdad5d823724722ae455d6975b33fd6db7 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Mon, 26 Feb 2018 20:47:58 +0000 Subject: [PATCH 16/38] part2 second exer done --- election-part2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/election-part2.js b/election-part2.js index 47f65af..0b3aa0e 100644 --- a/election-part2.js +++ b/election-part2.js @@ -17,7 +17,7 @@ class voter { this.votingCard = votingCard; } } -new new Voter('Jane Finnegan', 19, [1, 3]); +new Voter('Jane Finnegan', 19, [1, 3]); new Voter('Norman Beracha', 35, [3, 4]); new Voter('Salome Kadek', 22, [2, 1, 3]); new Vote('Wei Li', 19, [1, 2]); From 625f110f57bae69abe3b3f0f4a417bf2f6cfcbb7 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Mon, 26 Feb 2018 21:14:42 +0000 Subject: [PATCH 17/38] part2 second exer done --- election-part2.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/election-part2.js b/election-part2.js index 0b3aa0e..51b48b4 100644 --- a/election-part2.js +++ b/election-part2.js @@ -28,7 +28,8 @@ new Vote('Sam MacKinnon', 59, [1, 4]); * However they have some extra properties. */ class Candidate extends Voter { - constructor(party,numVotes) { + constructor(name, age, votingCard,party,numVotes) { + super(name, age, votingCard); this.party = party; this.numVotes = numVotes; } From 456f3f020de0d561678cfefd24dde5c9469f99a8 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 27 Feb 2018 12:34:59 +0000 Subject: [PATCH 18/38] part2 second exer done --- election-part2.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/election-part2.js b/election-part2.js index 51b48b4..da87126 100644 --- a/election-part2.js +++ b/election-part2.js @@ -17,11 +17,14 @@ class voter { this.votingCard = votingCard; } } -new Voter('Jane Finnegan', 19, [1, 3]); -new Voter('Norman Beracha', 35, [3, 4]); -new Voter('Salome Kadek', 22, [2, 1, 3]); -new Vote('Wei Li', 19, [1, 2]); -new Vote('Sam MacKinnon', 59, [1, 4]); +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 Vote('Wei Li', 19, [1, 2]), + new Vote('Sam MacKinnon', 59, [1, 4]) +] + /** * 2 - Write a Candidate class modelling a candidate in the election. Candidates are also voters (they can vote for themselves, or anyone else). @@ -34,10 +37,13 @@ class Candidate extends Voter { this.numVotes = numVotes; } } -new Candidate('Tamara Faiza', 46, 'Pizza Party', [1,1]); -new Candidate('Aylin Duke',39,'Foam Party', [2, 2]); -new Candidate( 'Clay Roderick',54,'Flat Earth Party',[3, 4]); -new Candidate('Nour al-Din',32, 'Pizza Party', [4, 1]); +let Candidates=[ + new Candidate('Tamara Faiza', 46, 'Pizza Party', [1,1]), + new Candidate('Aylin Duke',39,'Foam Party', [2, 2]), + new Candidate( 'Clay Roderick',54,'Flat Earth Party',[3, 4]), + new Candidate('Nour al-Din',32, 'Pizza Party', [4, 1]) +] + /** * 3 - Write an Election class which models the election. */ From 6cf81339a62f4995381bc5eed004a89703923bbd Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 27 Feb 2018 17:43:37 +0000 Subject: [PATCH 19/38] second exer modification of part 2 --- election-part2.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/election-part2.js b/election-part2.js index da87126..abc8c57 100644 --- a/election-part2.js +++ b/election-part2.js @@ -10,7 +10,7 @@ const { /** * 1 - Write a Voter class modelling a member of the population who votes in the election. */ -class voter { +class Voter { constructor(name, age, votingCard) { this.name = name; this.age = age; @@ -21,8 +21,8 @@ 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 Vote('Wei Li', 19, [1, 2]), - new Vote('Sam MacKinnon', 59, [1, 4]) + new Voter('Wei Li', 19, [1, 2]), + new Voter('Sam MacKinnon', 59, [1, 4]) ] @@ -44,6 +44,7 @@ let Candidates=[ new Candidate('Nour al-Din',32, 'Pizza Party', [4, 1]) ] + /** * 3 - Write an Election class which models the election. */ @@ -66,6 +67,8 @@ class Election{ } } + + // Include your votingPopulation array here. let votingPopulation = []; From a694df7b8c26a82a3f1ceec2c5accf1a4fe791de Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 27 Feb 2018 17:59:08 +0000 Subject: [PATCH 20/38] second exer modification of part 2 --- election-part2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/election-part2.js b/election-part2.js index abc8c57..c100d1a 100644 --- a/election-part2.js +++ b/election-part2.js @@ -37,7 +37,7 @@ class Candidate extends Voter { this.numVotes = numVotes; } } -let Candidates=[ +let candidates=[ new Candidate('Tamara Faiza', 46, 'Pizza Party', [1,1]), new Candidate('Aylin Duke',39,'Foam Party', [2, 2]), new Candidate( 'Clay Roderick',54,'Flat Earth Party',[3, 4]), From 36a877aaa8b0762f6d21bca406e90d92edf59503 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 27 Feb 2018 18:06:39 +0000 Subject: [PATCH 21/38] second exer modification of part 2 --- election-part2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/election-part2.js b/election-part2.js index c100d1a..3e93b64 100644 --- a/election-part2.js +++ b/election-part2.js @@ -70,11 +70,11 @@ class Election{ // Include your votingPopulation array here. -let votingPopulation = []; +//let votingPopulation = []; // Include your candidates object here. -let candidates = {}; +//let candidates = {}; let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); From 3d0ae7bef6260d3f2dd631e1891a44fb61b9ac46 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 27 Feb 2018 19:19:38 +0000 Subject: [PATCH 22/38] still modifying part 2 exer 2 --- election-part2.js | 4 ++-- election.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/election-part2.js b/election-part2.js index 3e93b64..9eb48e2 100644 --- a/election-part2.js +++ b/election-part2.js @@ -49,10 +49,10 @@ let candidates=[ * 3 - Write an Election class which models the election. */ class Election{ - constructor(validVoters,candidates,winner){ + constructor(validVoters,candidates){ this.validVoters=validVoters; this.candidates=candidates; - this.winner=winner; + this.winner = 'winner has not been chosen yet'; } runElection() { // altering the voting cards of the candidates class variable diff --git a/election.js b/election.js index 9373d44..1c3240f 100644 --- a/election.js +++ b/election.js @@ -106,7 +106,7 @@ let validVoters = filterInvalidVoters(allVoters); //console.log(validVoters,"validdddddddddd") candidates = runElection(validVoters, candidates); -console.log(candidates); +//console.log(candidates); let winner = getWinner(candidates); //console.log("and the winner is: ",winner) From fd12b9d3ff300f21ab651c9518a37d7f66081f66 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Sat, 3 Mar 2018 13:31:14 +0000 Subject: [PATCH 23/38] part2 almost done and part 1 refactored --- election-part2.js | 42 +++++++++++++++++------------- election.js | 36 +++++++++----------------- index.html | 33 ++++++++++++++++++++++-- styles.css | 66 +++++++++++++++++++++++++++++++++++++++++++++++ voter.js | 14 ++++++++++ voter.test.js | 36 ++++++++++++++++++++++++++ 6 files changed, 183 insertions(+), 44 deletions(-) create mode 100644 styles.css create mode 100644 voter.js create mode 100644 voter.test.js diff --git a/election-part2.js b/election-part2.js index 9eb48e2..8527472 100644 --- a/election-part2.js +++ b/election-part2.js @@ -31,43 +31,49 @@ let votingPopulation = [ * 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 = numVotes; + constructor(name, age, votingCard, party, numVotes) { + super(name, age, votingCard); + this.party = party; + this.numVotes = numVotes; } } -let candidates=[ - new Candidate('Tamara Faiza', 46, 'Pizza Party', [1,1]), - new Candidate('Aylin Duke',39,'Foam Party', [2, 2]), - new Candidate( 'Clay Roderick',54,'Flat Earth Party',[3, 4]), - new Candidate('Nour al-Din',32, 'Pizza Party', [4, 1]) -] +let candidates = { + 1: new Candidate('Tamara Faiza', 46, 'Pizza Party', [1, 1]), + 2: new Candidate('Aylin Duke', 39, 'Foam Party', [2, 2]), + 3: new Candidate('Clay Roderick', 54, 'Flat Earth Party', [3, 4]), + 4: new Candidate('Nour al-Din', 32, 'Pizza Party', [4, 1]) +} + /** * 3 - Write an Election class which models the election. */ -class Election{ - constructor(validVoters,candidates){ - this.validVoters=validVoters; - this.candidates=candidates; - this.winner = 'winner has not been chosen yet'; +class Election { + constructor(validVoters, candidates) { + this.validVoters = validVoters; + this.candidates = candidates; + this.winner = 'winner has not been chosen yet'; } runElection() { // altering the voting cards of the candidates class variable + this.candidates = runElection(this.validVoters, this.candidates) } getWinner() { //setting the winner class variable after the winner is calculated + this.winner = getWinner(this.candidates); } printWinnerMessage() { // should print a message as before including who won, //and how many votes he/she received - - } + if (this.winner === null) + {return "The election was a draw";} + return this.winner.name + " has won the election with " + this.winner.numVotes + " votes!"; + } + } - + // Include your votingPopulation array here. //let votingPopulation = []; diff --git a/election.js b/election.js index 1c3240f..c337bbf 100644 --- a/election.js +++ b/election.js @@ -16,9 +16,8 @@ function candidatesObjToArray(candidates) { */ function filterInvalidVoters(allVoters) { - return allVoters.filter(function (item) { - return item.votingCard.length < 3 && item.votingCard[0] !== item.votingCard[1] - }) + return allVoters.filter(item => item.votingCard.length < 3 && item.votingCard[0] !== item.votingCard[1] + ) } /** @@ -26,10 +25,6 @@ function filterInvalidVoters(allVoters) { * the right vote counts for half of the left vote. */ function runElection(validVoters, candidates) { - /* votes = []; - for (var i = 0; i < validVoters.length; i++) { - votes.push(validVoters[i].votingCard); - } */ votes = []; validVoters.forEach(function (item) { votes.push(item.votingCard) @@ -50,23 +45,16 @@ function runElection(validVoters, candidates) { * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} */ function getWinner(candidates) { - finalVotes = []; - for (var key in candidates) { - finalVotes.push(candidates[key].numVotes); - } - /* var min = Math.min(...finalVotes); - var max = Math.max(...finalVotes); - if (min === max) return null //if all candidates numVotes is equal so we have no winner */ - var max = Math.max(...finalVotes); - var maxDuplicate = [] - for (var i = 0; i < finalVotes.length; i++) { - if (finalVotes[i] === max) - maxDuplicate.push(finalVotes[i]); - } - if (maxDuplicate.length > 1) return null; - else - return candidates[Object.keys(candidates)[finalVotes.indexOf(max)]] - //horrible line above:returns the candidate connected to max based on key object, needs refactor! + let winner = {numVotes:0}; + Object.values(candidates).forEach(function (item) { + if (item.numVotes > winner.numVotes) { + winner = item; + } + else if (item.numVotes === winner.numVotes) { + return null + } + }) + return winner; } /** * 5 - Return a message including the name of the winner, and how many votes diff --git a/index.html b/index.html index e00ca0a..9656406 100644 --- a/index.html +++ b/index.html @@ -1,8 +1,37 @@ + + - - +

Welcome to Election Site

+
+
+

+ The list of Candidates +

+
    +
  • a
  • +
  • b
  • +
  • c
  • +
  • d
  • +
+
+
+

The List of Voters

+
    +
  • e
  • +
  • f
  • +
  • g
  • +
  • h
  • +
+
+ + +
+
+ + + \ No newline at end of file diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..6aaad0d --- /dev/null +++ b/styles.css @@ -0,0 +1,66 @@ +body{ + width:70%; + margin:auto; + text-align: center; + background-image: url('https://tinyurl.com/ya3yy5q9'); + color:white; +} + +.container { + background-color: azure; + border:1px solid red; + min-height:400px; + height:auto !important; /* for IE as it does not support min-height */ + opacity: 0.5; + border-radius: 10px; + padding:5px; +} +ul,h3{ + color:black; + font-weight: bold; + text-align: left; +} +li{ + text-align: left; +} +.candidate-column, .voters-column { + width:45%; + margin:1%; + padding:1%; + float:left; + background-color:#f392b0; + border-radius: 10px; +} + +.container:after{ + content:""; + display:block; + clear:float; +} +button{ + color:red; + background-color: rgb(241, 8, 58); + border: none; + color: rgb(8, 8, 8); + padding: 15px 32px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + margin: 4px 2px; + cursor: pointer; + font-weight: bold; + border-radius: 10px; +} +button:hover{ + background-color:yellow ; +} + +@media screen and (max-width: 600px) { + .voters-column , .candidate-column { + width: 90%; + } + .container{ + width:100%; + } +} \ No newline at end of file diff --git a/voter.js b/voter.js new file mode 100644 index 0000000..0b54457 --- /dev/null +++ b/voter.js @@ -0,0 +1,14 @@ +class Voter { + constructor(name, age, votingCard) { + + this.name = name; + this.age = age; + this.votingCard = votingCard; + } + valid() { + return this.votingCard.length < 3 && this.votingCard[0] !== this.votingCard[1] + } +} +module.exports = { + Voter +} \ No newline at end of file diff --git a/voter.test.js b/voter.test.js new file mode 100644 index 0000000..9126d1b --- /dev/null +++ b/voter.test.js @@ -0,0 +1,36 @@ +function filterInvalidVoters(allVoters) { + return allVoters.filter(item => item.valid()) +} +const { Voter } = require("./voter") +test("return only valid voters",()=> { + 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]) + ] + expect(filterInvalidVoters(votingPopulation)).toEqual([ + new Voter('Jane Finnegan', 19, [1, 3]), + new Voter('Norman Beracha', 35, [3, 4]), + new Voter('Wei Li', 19, [1, 2]), + new Voter('Sam MacKinnon', 59, [1, 4])] + ) +} + +) +test('if it has a name', () => { + let voter = new Voter("mahsa", "18", [1, 2, 3]); + expect(voter.name).toEqual( + "mahsa" + ) +}) + +test("if the voting card is valid",()=>{ + let voter=new Voter("mahsa", "18", [1, 2]); + expect(voter.valid()).toEqual(true); +}) +test("if the voting card is invalid",()=>{ + let voter=new Voter("mahsa", "18", [1, 2,3]); + expect(voter.valid()).toEqual(false); +}) \ No newline at end of file From 7766b82ef6c8250f720d05a62fc9b1286551bb46 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 6 Mar 2018 14:23:49 +0000 Subject: [PATCH 24/38] last changes --- election-part2.js | 21 ++++++++++++--------- election.js | 19 +++++++++++++------ election.test.js | 10 ++++++++++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/election-part2.js b/election-part2.js index 8527472..0501bd9 100644 --- a/election-part2.js +++ b/election-part2.js @@ -66,11 +66,10 @@ class Election { } printWinnerMessage() { // should print a message as before including who won, //and how many votes he/she received - if (this.winner === null) - {return "The election was a draw";} + if (this.winner === null) { return "The election was a draw"; } return this.winner.name + " has won the election with " + this.winner.numVotes + " votes!"; - } - + } + } @@ -82,13 +81,17 @@ class Election { // Include your candidates object here. //let candidates = {}; +window.onload = function () { + let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); + + let validVoters = filterInvalidVoters(allVoters); -let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); + let election = new Election(validVoters, candidates); -let validVoters = filterInvalidVoters(allVoters); + election.runElection(); // Example of how runElection() can be called. -let election = new Election(validVoters, candidates); + console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. + +} -election.runElection(); // Example of how runElection() can be called. -console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. diff --git a/election.js b/election.js index c337bbf..6d47076 100644 --- a/election.js +++ b/election.js @@ -45,15 +45,22 @@ function runElection(validVoters, candidates) { * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} */ function getWinner(candidates) { - let winner = {numVotes:0}; + let winner = {numVotes:0.0}; + try{ Object.values(candidates).forEach(function (item) { - if (item.numVotes > winner.numVotes) { - winner = item; + /* console.log(item.name,item.numVotes); + console.log(winner.name,winner.numVotes); + console.log(); */ + if (item.numVotes == winner.numVotes) { + throw "Duplicate"; } - else if (item.numVotes === winner.numVotes) { - return null + else if (item.numVotes > winner.numVotes) { + winner = item; } }) + }catch(error){ + return null; + } return winner; } /** @@ -84,7 +91,7 @@ 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 } + 4: { name: 'Nour al-Din', age: 32, votingCard: [4, 3], party: 'Pizza Party', numVotes: 0 } }; let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); diff --git a/election.test.js b/election.test.js index 75efd0b..f1f3864 100644 --- a/election.test.js +++ b/election.test.js @@ -56,6 +56,16 @@ test('getWinner should return the winning candidate', () => { numVotes: 3.5, }) }) +test('getWinner should return the winning candidate', () => { + const candidatesAfterElection = { + 1: {name:"Tamara Faiza",age:46,votingCard:[1,1],party:"Pizza Party",numVotes:3.0}, + 2: {name:"Aylin Duke",age:39,votingCard:[2,2],party:"Foam Party",numVotes:0.5}, + 3: {name:"Clay Roderick",age:54,votingCard:[3,4],party:"Flat Earth Party",numVotes:3.0}, + 4: {name:"Nour al-Din",age:32,votingCard:[4,1],party:"Pizza Party",numVotes:2.5} + }; + + expect(getWinner(candidatesAfterElection)).toEqual(null) +}) test('winnerMessage should return a message with the name of the winner and number of votes received', () => { const winner = { From 3771e6c7e7d2b57d54d25e73ae40f99978d475fa Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Fri, 9 Mar 2018 17:41:47 +0000 Subject: [PATCH 25/38] part 2 still has to do --- election-part2.js | 16 +++++++++---- election.js | 53 +++++++++++++++++++++++++---------------- electionclass.js | 20 ++++++++++++++++ electionclass.test.js | 55 +++++++++++++++++++++++++++++++++++++++++++ voter.test.js | 1 - 5 files changed, 119 insertions(+), 26 deletions(-) create mode 100644 electionclass.js create mode 100644 electionclass.test.js diff --git a/election-part2.js b/election-part2.js index 0501bd9..1a7f1e3 100644 --- a/election-part2.js +++ b/election-part2.js @@ -56,16 +56,12 @@ class Election { this.winner = 'winner has not been chosen yet'; } runElection() { - // altering the voting cards of the candidates class variable this.candidates = runElection(this.validVoters, this.candidates) } getWinner() { - //setting the winner class variable after the winner is calculated this.winner = getWinner(this.candidates); } printWinnerMessage() { - // should print a message as before including who won, - //and how many votes he/she received if (this.winner === null) { return "The election was a draw"; } return this.winner.name + " has won the election with " + this.winner.numVotes + " votes!"; } @@ -81,7 +77,7 @@ class Election { // Include your candidates object here. //let candidates = {}; -window.onload = function () { + let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); let validVoters = filterInvalidVoters(allVoters); @@ -92,6 +88,16 @@ window.onload = function () { console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. +/* window.onload = function () { + candidateList=document.getElementsByClassName("candidates"); + updatehtml(); + } +function updatehtml(){ + candidateList. +} */ +fetch('http://www.mocky.io/v2/5a55224b2d000088425b1ed8') +.then(function(response){return response.json()}) +.then(function(json){console.log(json);}) diff --git a/election.js b/election.js index 6d47076..56f82a2 100644 --- a/election.js +++ b/election.js @@ -24,7 +24,7 @@ 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(validVoters, candidates) { +/* function runElection(validVoters, candidates) { votes = []; validVoters.forEach(function (item) { votes.push(item.votingCard) @@ -38,6 +38,19 @@ function runElection(validVoters, candidates) { } } return candidates; +} */ +//refactored Version of runElection +function runElection(validVoters, candidates) { + for (let i = 1; i < Object.values(candidates).length; i++) { + validVoters.forEach(function (item) { + if (item.votingCard[0] === i) { + candidates[i].numVotes += 1 + } + if (item.votingCard[1] === i) + candidates[i].numVotes += 0.5; + }) + } + return candidates; } /** * 4 - After an election has been run, return the winner @@ -45,22 +58,22 @@ function runElection(validVoters, candidates) { * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} */ function getWinner(candidates) { - let winner = {numVotes:0.0}; - try{ - Object.values(candidates).forEach(function (item) { - /* console.log(item.name,item.numVotes); - console.log(winner.name,winner.numVotes); - console.log(); */ - if (item.numVotes == winner.numVotes) { - throw "Duplicate"; - } - else if (item.numVotes > winner.numVotes) { - winner = item; - } - }) - }catch(error){ - return null; - } + let winner = { numVotes: 0.0 }; + try { + Object.values(candidates).forEach(function (item) { + /* console.log(item.name,item.numVotes); + console.log(winner.name,winner.numVotes); + console.log(); */ + if (item.numVotes == winner.numVotes) { + throw "Duplicate"; + } + else if (item.numVotes > winner.numVotes) { + winner = item; + } + }) + } catch (error) { + return null; + } return winner; } /** @@ -91,14 +104,14 @@ 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, 3], party: 'Pizza Party', numVotes: 0 } -}; + 4: { name: 'Nour al-Din', age: 32, votingCard: [4, 1], party: 'Pizza Party', numVotes: 0 } +} //4,3 is draw let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); //console.log(allVoters); let validVoters = filterInvalidVoters(allVoters); -//console.log(validVoters,"validdddddddddd") +//console.log(validVoters,"validdddddddddd") //--> array of Objects candidates = runElection(validVoters, candidates); //console.log(candidates); diff --git a/electionclass.js b/electionclass.js new file mode 100644 index 0000000..bed1df8 --- /dev/null +++ b/electionclass.js @@ -0,0 +1,20 @@ +class Election { + constructor(validVoters, candidates) { + this.validVoters = validVoters; + this.candidates = candidates; + this.winner = 'winner has not been chosen yet'; + } + runElection() { + this.candidates = runElection(this.validVoters, this.candidates) + + } getWinner() { + this.winner = getWinner(this.candidates); + + } printWinnerMessage() { + if (this.winner === null) { return "The election was a draw"; } + return this.winner.name + " has won the election with " + this.winner.numVotes + " votes!"; + } +} +module.exports = { + Election +} \ No newline at end of file diff --git a/electionclass.test.js b/electionclass.test.js new file mode 100644 index 0000000..12ffa94 --- /dev/null +++ b/electionclass.test.js @@ -0,0 +1,55 @@ +function runElection(validVoters, candidates) { + return candidates.map(item=>item.runElection()); //?map????? +} + +function getWinner(candidates) { + let winner = { numVotes: 0.0 }; + try { + Object.values(candidates).forEach(function (item) { + if (item.numVotes == winner.numVotes) { + throw "Duplicate"; + } + else if (item.numVotes > winner.numVotes) { + winner = item; + } + }) + } catch (error) { + return null; + } + return winner; +} +const { Election } = require("./electionclass.js") +test('running the election should add votes to the candidates', () => { + const validVoters = [ + {name: 'Jane Finnegan', age: 19, votingCard: [1,3]}, + {name: 'Norman Beracha', age: 35, votingCard: [3,4]}, + {name: 'Wei Li', age: 19, votingCard: [1,2]}, + {name: 'Sam MacKinnon', age: 59, votingCard: [1,4]}, + {name: 'Clay Roderick', age: 54, votingCard: [3,4], party: 'Flat Earth Party', numVotes: 0}, + {name: 'Nour al-Din', age: 32, votingCard: [4,1], party: 'Pizza Party', numVotes: 0} + ] + + expect(runElection(validVoters, candidates)).toEqual({ + 1: {name:"Tamara Faiza",age:46,votingCard:[1,1],party:"Pizza Party",numVotes:3.5}, + 2: {name:"Aylin Duke",age:39,votingCard:[2,2],party:"Foam Party",numVotes:0.5}, + 3: {name:"Clay Roderick",age:54,votingCard:[3,4],party:"Flat Earth Party",numVotes:2.5}, + 4: {name:"Nour al-Din",age:32,votingCard:[4,1],party:"Pizza Party",numVotes:2.5} + }) +}) + +test('getWinner should return the winning candidate', () => { + const candidatesAfterElection = { + 1: {name:"Tamara Faiza",age:46,votingCard:[1,1],party:"Pizza Party",numVotes:3.5}, + 2: {name:"Aylin Duke",age:39,votingCard:[2,2],party:"Foam Party",numVotes:0.5}, + 3: {name:"Clay Roderick",age:54,votingCard:[3,4],party:"Flat Earth Party",numVotes:2.5}, + 4: {name:"Nour al-Din",age:32,votingCard:[4,1],party:"Pizza Party",numVotes:2.5} + }; + + expect(getWinner(candidatesAfterElection)).toEqual({ + name: 'Tamara Faiza', + age: 46, + votingCard: [1, 1], + party: 'Pizza Party', + numVotes: 3.5, + }) +}) \ No newline at end of file diff --git a/voter.test.js b/voter.test.js index 9126d1b..47c5b10 100644 --- a/voter.test.js +++ b/voter.test.js @@ -17,7 +17,6 @@ test("return only valid voters",()=> { new Voter('Sam MacKinnon', 59, [1, 4])] ) } - ) test('if it has a name', () => { let voter = new Voter("mahsa", "18", [1, 2, 3]); From ca7ee7c99d5d9306939f31df38e02ba185453165 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Sat, 10 Mar 2018 13:05:28 +0000 Subject: [PATCH 26/38] i added run election to my part2 --- election-part2.js | 111 +++++++++++++++++++++++++++++++++++++--------- election.js | 16 ------- 2 files changed, 90 insertions(+), 37 deletions(-) diff --git a/election-part2.js b/election-part2.js index 1a7f1e3..4714db6 100644 --- a/election-part2.js +++ b/election-part2.js @@ -1,11 +1,79 @@ // Importing the functions from what you did in part 1. -const { +function candidatesObjToArray(candidates) { + var arrOfKeys = Object.keys(candidates); + var arrOfCandidates = arrOfKeys.map(function (key) { return candidates[key]; }); + return arrOfCandidates; +} +/** + * 2 - Remove any voters who have voted for more than 2 people, or have voted for the same person twice. +*/ +function filterInvalidVoters(allVoters) { + return allVoters.filter(item => item.votingCard.length < 3 && item.votingCard[0] !== item.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. + */ +//refactored Version of runElection +function runElection(validVoters, candidates) { + //console.log(validVoters,"validVoters") + //console.log(candidates,"candidates") + for (let i = 1; i < Object.values(candidates).length; i++) { + validVoters.forEach(function (item) { + if (item.votingCard[0] === i) { + candidates[i].numVotes += 1 + } + if (item.votingCard[1] === i) + candidates[i].numVotes += 0.5; + }) + } + //console.log("after runElec",candidates) + return candidates; +} +/** + * 4 - After an election has been run, return the winner + * + * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} + */ +function getWinner(candidates) { + let winner = { numVotes: 0.0 }; + try { + Object.values(candidates).forEach(function (item) { + /* console.log(item.name,item.numVotes); + console.log(winner.name,winner.numVotes); + console.log(); */ + if (item.numVotes == winner.numVotes) { + throw "Duplicate"; + } + else if (item.numVotes > winner.numVotes) { + winner = item; + } + }) + } catch (error) { + return null; + } + 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 mes = winner.name + " has won the election with " + winner.numVotes + " votes!"; + return mes; + } + else return "The election was a draw"; +} +/* const { candidatesObjToArray, filterInvalidVoters, runElection, getWinner, winnerMessage, -} = require('./election'); +} = require('./election'); */ /** * 1 - Write a Voter class modelling a member of the population who votes in the election. @@ -24,17 +92,15 @@ let votingPopulation = [ new Voter('Wei Li', 19, [1, 2]), new Voter('Sam MacKinnon', 59, [1, 4]) ] - - /** * 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) { + constructor(name, age, votingCard, party) { super(name, age, votingCard); this.party = party; - this.numVotes = numVotes; + this.numVotes = 0; } } let candidates = { @@ -43,9 +109,6 @@ let candidates = { 3: new Candidate('Clay Roderick', 54, 'Flat Earth Party', [3, 4]), 4: new Candidate('Nour al-Din', 32, 'Pizza Party', [4, 1]) } - - - /** * 3 - Write an Election class which models the election. */ @@ -56,20 +119,21 @@ class Election { this.winner = 'winner has not been chosen yet'; } runElection() { + //console.log("runElec is getting called"); this.candidates = runElection(this.validVoters, this.candidates) + // console.log("runElec is getting called after",this.candidates); } getWinner() { + //console.log("getwinner is getting called") this.winner = getWinner(this.candidates); } printWinnerMessage() { + //console.log("this is bug message",this.winner) if (this.winner === null) { return "The election was a draw"; } return this.winner.name + " has won the election with " + this.winner.numVotes + " votes!"; } } - - - // Include your votingPopulation array here. //let votingPopulation = []; @@ -78,15 +142,15 @@ class Election { //let candidates = {}; - let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); - - let validVoters = filterInvalidVoters(allVoters); +let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); - let election = new Election(validVoters, candidates); +let validVoters = filterInvalidVoters(allVoters); - election.runElection(); // Example of how runElection() can be called. +let election = new Election(validVoters, candidates); - console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. +election.runElection(); // Example of how runElection() can be called. +election.getWinner(); +console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. /* window.onload = function () { candidateList=document.getElementsByClassName("candidates"); @@ -96,8 +160,13 @@ class Election { function updatehtml(){ candidateList. } */ - +/* fetch('http://www.mocky.io/v2/5a55224b2d000088425b1ed8') -.then(function(response){return response.json()}) -.then(function(json){console.log(json);}) + .then(function (response) { return response.json() }) + .then(function (json) { console.log(json); }) */ + +/* let listCan=document.querySelectorAll("candidates"); +for (let i=0;i item.votingCard.length < 3 && item.votingCard[0] !== item.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(validVoters, candidates) { - votes = []; - validVoters.forEach(function (item) { - votes.push(item.votingCard) - }) - for (var i = 0; i < votes.length; i++) { //loop through vote array - for (var j = 1; j < 5; j++) { //loop through candidates array - if (votes[i][0] === j) - candidates[j].numVotes += 1; - if (votes[i][1] === j) - candidates[j].numVotes += 0.5 - } - } - return candidates; -} */ //refactored Version of runElection function runElection(validVoters, candidates) { for (let i = 1; i < Object.values(candidates).length; i++) { From d31117f8d0daa34d66294cab532b8dafd0f275de Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Sat, 10 Mar 2018 15:12:49 +0000 Subject: [PATCH 27/38] i changed the order of class variables in candidate class at line 32 --- election-part2.js | 84 +++++------------------------------------------ nefile | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 75 deletions(-) create mode 100644 nefile diff --git a/election-part2.js b/election-part2.js index 4714db6..573f1ed 100644 --- a/election-part2.js +++ b/election-part2.js @@ -1,82 +1,14 @@ // Importing the functions from what you did in part 1. -function candidatesObjToArray(candidates) { - var arrOfKeys = Object.keys(candidates); - var arrOfCandidates = arrOfKeys.map(function (key) { return candidates[key]; }); - return arrOfCandidates; -} -/** - * 2 - Remove any voters who have voted for more than 2 people, or have voted for the same person twice. -*/ -function filterInvalidVoters(allVoters) { - return allVoters.filter(item => item.votingCard.length < 3 && item.votingCard[0] !== item.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. - */ -//refactored Version of runElection -function runElection(validVoters, candidates) { - //console.log(validVoters,"validVoters") - //console.log(candidates,"candidates") - for (let i = 1; i < Object.values(candidates).length; i++) { - validVoters.forEach(function (item) { - if (item.votingCard[0] === i) { - candidates[i].numVotes += 1 - } - if (item.votingCard[1] === i) - candidates[i].numVotes += 0.5; - }) - } - //console.log("after runElec",candidates) - return candidates; -} -/** - * 4 - After an election has been run, return the winner - * - * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} - */ -function getWinner(candidates) { - let winner = { numVotes: 0.0 }; - try { - Object.values(candidates).forEach(function (item) { - /* console.log(item.name,item.numVotes); - console.log(winner.name,winner.numVotes); - console.log(); */ - if (item.numVotes == winner.numVotes) { - throw "Duplicate"; - } - else if (item.numVotes > winner.numVotes) { - winner = item; - } - }) - } catch (error) { - return null; - } - 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 mes = winner.name + " has won the election with " + winner.numVotes + " votes!"; - return mes; - } - else return "The election was a draw"; -} -/* const { + + const { candidatesObjToArray, filterInvalidVoters, runElection, getWinner, winnerMessage, -} = require('./election'); */ +} = require('./election'); -/** - * 1 - Write a Voter class modelling a member of the population who votes in the election. + /* 1 - Write a Voter class modelling a member of the population who votes in the election. */ class Voter { constructor(name, age, votingCard) { @@ -97,7 +29,7 @@ let votingPopulation = [ * However they have some extra properties. */ class Candidate extends Voter { - constructor(name, age, votingCard, party) { + constructor(name, age, party,votingCard) { super(name, age, votingCard); this.party = party; this.numVotes = 0; @@ -115,13 +47,14 @@ let candidates = { class Election { constructor(validVoters, candidates) { this.validVoters = validVoters; + //console.log(validVoters, "validVoters in class"); this.candidates = candidates; this.winner = 'winner has not been chosen yet'; } runElection() { //console.log("runElec is getting called"); this.candidates = runElection(this.validVoters, this.candidates) - // console.log("runElec is getting called after",this.candidates); + // console.log("runElec is getting called after",this.candidates); } getWinner() { //console.log("getwinner is getting called") @@ -143,9 +76,10 @@ class Election { let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); - +console.log(allVoters, "allvoters invoke") let validVoters = filterInvalidVoters(allVoters); + let election = new Election(validVoters, candidates); election.runElection(); // Example of how runElection() can be called. diff --git a/nefile b/nefile new file mode 100644 index 0000000..30908bd --- /dev/null +++ b/nefile @@ -0,0 +1,73 @@ +/* function candidatesObjToArray(candidates) { + var arrOfKeys = Object.keys(candidates); + var arrOfCandidates = arrOfKeys.map(function (key) { return candidates[key] }); + return arrOfCandidates; +} +/** + * 2 - Remove any voters who have voted for more than 2 people, or have voted for the same person twice. +*/ +function filterInvalidVoters(allVoters) { + const filteredByLength = allVoters.filter(item => item.votingCard.length < 3); + console.log('first filter', filteredByLength); + const filteredByClash = filteredByLength.filter(item => item.votingCard[0] !== item.votingCard[1]) + console.log('second filter',filteredByClash) + return filteredByClash; + } +/** + * 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. + */ +//refactored Version of runElection +function runElection(validVoters, candidates) { + // console.log(validVoters, "validVoters") + //console.log(candidates,"candidates") + for (let i = 1; i <= Object.values(candidates).length; i++) { + //console.log("candidate", i) + validVoters.forEach(function (item) { + //console.log(item); + if (item.votingCard[0] === i) { + candidates[i].numVotes += 1 + } + if (item.votingCard[1] === i) + candidates[i].numVotes += 0.5; + }) + } + //console.log("after runElec",candidates) + return candidates; +} +/** + * 4 - After an election has been run, return the winner + * + * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} + */ +function getWinner(candidates) { + let winner = { numVotes: 0.0 }; + try { + Object.values(candidates).forEach(function (item) { + /* console.log(item.name,item.numVotes); + console.log(winner.name,winner.numVotes); + console.log(); */ + if (item.numVotes == winner.numVotes) { + throw "Duplicate"; + } + else if (item.numVotes > winner.numVotes) { + winner = item; + } + }) + } catch (error) { + return null; + } + 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 mes = winner.name + " has won the election with " + winner.numVotes + " votes!"; + return mes; + } + else return "The election was a draw"; +} */ \ No newline at end of file From 5a86a68aa8f221822326dd4e46944997e76e01d0 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 13 Mar 2018 15:20:38 +0000 Subject: [PATCH 28/38] button and static lists of cand and voters using dom provided --- election-part2.js | 126 ++++++++++++++++++++++++++++++++++++---------- index.html | 7 ++- 2 files changed, 102 insertions(+), 31 deletions(-) diff --git a/election-part2.js b/election-part2.js index 573f1ed..1babafe 100644 --- a/election-part2.js +++ b/election-part2.js @@ -1,15 +1,85 @@ // Importing the functions from what you did in part 1. - const { - candidatesObjToArray, - filterInvalidVoters, - runElection, - getWinner, - winnerMessage, -} = require('./election'); - - /* 1 - Write a Voter class modelling a member of the population who votes in the election. +// const { +// candidatesObjToArray, +// filterInvalidVoters, +// runElection, +// getWinner, +// winnerMessage, +// } = require('./election'); +function candidatesObjToArray(candidates) { + var arrOfKeys = Object.keys(candidates); + var arrOfCandidates = arrOfKeys.map(function (key) { return candidates[key] }); + return arrOfCandidates; +} +/** + * 2 - Remove any voters who have voted for more than 2 people, or have voted for the same person twice. +*/ +function filterInvalidVoters(allVoters) { + const filteredByLength = allVoters.filter(item => item.votingCard.length < 3); + const filteredByClash = filteredByLength.filter(item => item.votingCard[0] !== item.votingCard[1]) + return filteredByClash; +} +/** + * 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. */ +//refactored Version of runElection +function runElection(validVoters, candidates) { + // console.log(validVoters, "validVoters") + //console.log(candidates,"candidates") + for (let i = 1; i <= Object.values(candidates).length; i++) { + //console.log("candidate", i) + validVoters.forEach(function (item) { + //console.log(item); + if (item.votingCard[0] === i) { + candidates[i].numVotes += 1 + } + if (item.votingCard[1] === i) + candidates[i].numVotes += 0.5; + }) + } + //console.log("after runElec",candidates) + return candidates; +} +/** + * 4 - After an election has been run, return the winner + * + * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} + */ +function getWinner(candidates) { + let winner = { numVotes: 0.0 }; + try { + Object.values(candidates).forEach(function (item) { + /* console.log(item.name,item.numVotes); + console.log(winner.name,winner.numVotes); + console.log(); */ + if (item.numVotes == winner.numVotes) { + throw "Duplicate"; + } + else if (item.numVotes > winner.numVotes) { + winner = item; + } + }) + } catch (error) { + return null; + } + 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 mes = winner.name + " has won the election with " + winner.numVotes + " votes!"; + return mes; + } + else return "The election was a draw"; +} +/* 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; @@ -29,7 +99,7 @@ let votingPopulation = [ * However they have some extra properties. */ class Candidate extends Voter { - constructor(name, age, party,votingCard) { + constructor(name, age, party, votingCard) { super(name, age, votingCard); this.party = party; this.numVotes = 0; @@ -70,37 +140,39 @@ class Election { // Include your votingPopulation array here. //let votingPopulation = []; - // Include your candidates object here. //let candidates = {}; - let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); -console.log(allVoters, "allvoters invoke") let validVoters = filterInvalidVoters(allVoters); - let election = new Election(validVoters, candidates); election.runElection(); // Example of how runElection() can be called. election.getWinner(); console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. -/* window.onload = function () { - candidateList=document.getElementsByClassName("candidates"); - updatehtml(); - -} -function updatehtml(){ - candidateList. -} */ /* fetch('http://www.mocky.io/v2/5a55224b2d000088425b1ed8') .then(function (response) { return response.json() }) .then(function (json) { console.log(json); }) */ -/* let listCan=document.querySelectorAll("candidates"); -for (let i=0;i alert(election.printWinnerMessage())); + +//list of Candidates +let listCan = document.querySelectorAll(".candidates li"); +// listCan[0].textContent=candidates["1"].name; +// listCan[1].textContent=candidates["2"].name; +// listCan[2].textContent=candidates["3"].name; +// listCan[3].textContent=candidates["4"].name; + +//listCan.forEach((item,i,candidates)=>item[i].textContent=candidates[i+1].name); //????? +listCan.forEach((item, i, listCan) => item.textContent = candidates[i + 1].name) +// for (let i = 0; i < listCan.length; i++) { +// listCan[i].textContent = candidates[i + 1].name; +// } +//list of Voters +let listVote = document.querySelectorAll(".voters li"); +listVote.forEach((item,i ,listVote)=> item.textContent = votingPopulation[i].name); \ No newline at end of file diff --git a/index.html b/index.html index 9656406..e511724 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,6 @@ - @@ -26,12 +25,12 @@

The List of Voters

  • f
  • g
  • h
  • +
  • i
  • - - +
    - + \ No newline at end of file From 5daab0224bf13bacd6e83bfdb4198ecf46a2139e Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 13 Mar 2018 16:06:52 +0000 Subject: [PATCH 29/38] debug commit to check what changed --- election-part2.js | 2 +- index.html | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/election-part2.js b/election-part2.js index 1babafe..d768935 100644 --- a/election-part2.js +++ b/election-part2.js @@ -175,4 +175,4 @@ listCan.forEach((item, i, listCan) => item.textContent = candidates[i + 1].name) // } //list of Voters let listVote = document.querySelectorAll(".voters li"); -listVote.forEach((item,i ,listVote)=> item.textContent = votingPopulation[i].name); \ No newline at end of file +listVote.forEach((item, listVote)=> item.textContent = votingPopulation[i].name); \ No newline at end of file diff --git a/index.html b/index.html index e511724..4ff5d20 100644 --- a/index.html +++ b/index.html @@ -16,6 +16,7 @@

  • b
  • c
  • d
  • +
  • e
  • @@ -26,11 +27,12 @@

    The List of Voters

  • g
  • h
  • i
  • +

    - + \ No newline at end of file From f166fe589ff0b7a64c4ddee004a344a4eed1dbf1 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 13 Mar 2018 16:10:48 +0000 Subject: [PATCH 30/38] debug commit to check what changed --- election-part2.js | 2 +- index.html | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/election-part2.js b/election-part2.js index d768935..210c510 100644 --- a/election-part2.js +++ b/election-part2.js @@ -175,4 +175,4 @@ listCan.forEach((item, i, listCan) => item.textContent = candidates[i + 1].name) // } //list of Voters let listVote = document.querySelectorAll(".voters li"); -listVote.forEach((item, listVote)=> item.textContent = votingPopulation[i].name); \ No newline at end of file +listVote.forEach((item, i, listVote)=> item.textContent = votingPopulation[i].name); \ No newline at end of file diff --git a/index.html b/index.html index 4ff5d20..350c06a 100644 --- a/index.html +++ b/index.html @@ -35,4 +35,5 @@

    The List of Voters


    + \ No newline at end of file From e0bd61505a5ad9d226380a1663480eb43018f6fb Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 13 Mar 2018 16:22:49 +0000 Subject: [PATCH 31/38] issue if i add an li tag to cand list --- election-part2.js | 2 +- index.html | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/election-part2.js b/election-part2.js index 210c510..1a47928 100644 --- a/election-part2.js +++ b/election-part2.js @@ -168,7 +168,7 @@ let listCan = document.querySelectorAll(".candidates li"); // listCan[2].textContent=candidates["3"].name; // listCan[3].textContent=candidates["4"].name; -//listCan.forEach((item,i,candidates)=>item[i].textContent=candidates[i+1].name); //????? +//listCan.forEach((item,i,candidates)=>item[i].textContent=candidates[i+1].name); //?????solved listCan.forEach((item, i, listCan) => item.textContent = candidates[i + 1].name) // for (let i = 0; i < listCan.length; i++) { // listCan[i].textContent = candidates[i + 1].name; diff --git a/index.html b/index.html index 350c06a..01264ad 100644 --- a/index.html +++ b/index.html @@ -16,7 +16,7 @@

  • b
  • c
  • d
  • -
  • e
  • +
    @@ -27,7 +27,7 @@

    The List of Voters

  • g
  • h
  • i
  • - +
  • j
  • @@ -35,5 +35,5 @@

    The List of Voters


    - + \ No newline at end of file From cca27d6cb561c90c41846d13a3f57d1d81d8f7e4 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Wed, 14 Mar 2018 15:05:03 +0000 Subject: [PATCH 32/38] xmlhttprequest provided but not logs data yet --- election-part2.js | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/election-part2.js b/election-part2.js index 1a47928..3426f95 100644 --- a/election-part2.js +++ b/election-part2.js @@ -135,14 +135,7 @@ class Election { if (this.winner === null) { return "The election was a draw"; } return this.winner.name + " has won the election with " + this.winner.numVotes + " votes!"; } - } -// Include your votingPopulation array here. -//let votingPopulation = []; - -// Include your candidates object here. -//let candidates = {}; - let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); let validVoters = filterInvalidVoters(allVoters); @@ -151,7 +144,7 @@ let election = new Election(validVoters, candidates); election.runElection(); // Example of how runElection() can be called. election.getWinner(); console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. - +console.log("gggggggggggggg") /* fetch('http://www.mocky.io/v2/5a55224b2d000088425b1ed8') .then(function (response) { return response.json() }) @@ -175,4 +168,22 @@ listCan.forEach((item, i, listCan) => item.textContent = candidates[i + 1].name) // } //list of Voters let listVote = document.querySelectorAll(".voters li"); -listVote.forEach((item, i, listVote)=> item.textContent = votingPopulation[i].name); \ No newline at end of file +listVote.forEach((item, i, listVote)=> item.textContent = votingPopulation[i].name); + + +let ep = "https://www.mocky.io/v2/5a55224b2d000088425b1ed8" +function sendRequest(url) { + var xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = function () { + if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { + var data = JSON.parse(xmlhttp.responseText); + console.log(data.candidates[0].votingCard); + } + } + xmlhttp.open("GET", url, true); + xmlhttp.setRequestHeader("Access-Control-Allow-Origin","https://geojsonlint.com/") + xmlhttp.send(); +} + sendRequest(ep) +console.log(data.candidates[0].votingCard) + From e96251844544b17f8a98498089c43b8618ffcf69 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Mon, 19 Mar 2018 13:59:40 +0000 Subject: [PATCH 33/38] tried working with json --- election-part2.js | 49 ++++++++++----------- index.html | 3 +- nefile | 106 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 128 insertions(+), 30 deletions(-) diff --git a/election-part2.js b/election-part2.js index 3426f95..1207ac7 100644 --- a/election-part2.js +++ b/election-part2.js @@ -78,6 +78,7 @@ function winnerMessage(winner) { } else return "The election was a draw"; } + /* 1 - Write a Voter class modelling a member of the population who votes in the election. */ class Voter { @@ -136,24 +137,12 @@ class Election { return this.winner.name + " has won the election with " + this.winner.numVotes + " votes!"; } } -let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); -let validVoters = filterInvalidVoters(allVoters); - -let election = new Election(validVoters, candidates); - -election.runElection(); // Example of how runElection() can be called. -election.getWinner(); -console.log(election.printWinnerMessage()); // Example of how the winner message can be printed. -console.log("gggggggggggggg") + // Example of how the winner message can be printed. /* fetch('http://www.mocky.io/v2/5a55224b2d000088425b1ed8') .then(function (response) { return response.json() }) .then(function (json) { console.log(json); }) */ -//run Election Buttun -let runButt = document.getElementById("runBut"); -runButt.addEventListener("click", () => alert(election.printWinnerMessage())); - //list of Candidates let listCan = document.querySelectorAll(".candidates li"); // listCan[0].textContent=candidates["1"].name; @@ -170,20 +159,26 @@ listCan.forEach((item, i, listCan) => item.textContent = candidates[i + 1].name) let listVote = document.querySelectorAll(".voters li"); listVote.forEach((item, i, listVote)=> item.textContent = votingPopulation[i].name); +//run Election Buttun +let runButt = document.getElementById("runBut"); +runButt.addEventListener("click", () => alert(election.printWinnerMessage())); -let ep = "https://www.mocky.io/v2/5a55224b2d000088425b1ed8" -function sendRequest(url) { - var xmlhttp = new XMLHttpRequest(); - xmlhttp.onreadystatechange = function () { - if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { - var data = JSON.parse(xmlhttp.responseText); - console.log(data.candidates[0].votingCard); - } - } - xmlhttp.open("GET", url, true); - xmlhttp.setRequestHeader("Access-Control-Allow-Origin","https://geojsonlint.com/") - xmlhttp.send(); +function fetchElectionData(){ +var data; +fetch('https://www.mocky.io/v2/5a55224b2d000088425b1ed8').then(res => res.json()).then(function(jsonData){ data = jsonData; +candidates = data.candidates; +voters = data.voters; +let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); +let validVoters = filterInvalidVoters(allVoters); +console.log(validVoters,"inside fetchfunction") + +let election = new Election(validVoters, candidates); +election.runElection(); // Example of how runElection() can be called. +election.getWinner(); +console.log(candidates) +console.log(election.printWinnerMessage(),"inside fetchfunction");} +) } - sendRequest(ep) -console.log(data.candidates[0].votingCard) +fetchElectionData(); +//console.log(candidates,"last line") diff --git a/index.html b/index.html index 01264ad..10ca65d 100644 --- a/index.html +++ b/index.html @@ -16,7 +16,7 @@

  • b
  • c
  • d
  • - +
    @@ -27,7 +27,6 @@

    The List of Voters

  • g
  • h
  • i
  • -
  • j
  • diff --git a/nefile b/nefile index 30908bd..beb3be1 100644 --- a/nefile +++ b/nefile @@ -70,4 +70,108 @@ function winnerMessage(winner) { return mes; } else return "The election was a draw"; -} */ \ No newline at end of file +} */ + +let urll = "https://www.mocky.io/v2/5a55224b2d000088425b1ed8" +// function sendRequest(url) { +// var xmlhttp = new XMLHttpRequest(); +// xmlhttp.onreadystatechange = function () { +// if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { +// var data = JSON.parse(xmlhttp.responseText); +// console.log(data); +// } +// } +// xmlhttp.open("GET", url, true); +// xmlhttp.setRequestHeader("Access-Control-Allow-Origin","https://geojsonlint.com/") +// xmlhttp.send(); +// } +// sendRequest(urll) + +// console.log("hi") +// var ourReq=new XMLHttpRequest(); +// console.log("hi2") +// ourReq.open('GET','https://www.mocky.io/v2/5a55224b2d000088425b1ed8') +// ourReq.setRequestHeader("Access-Control-Allow-Origin","https://geojsonlint.com/") +// ourReq.onload=function(){ +// var ourData=JSON.parse(ourReq.responseText) +// console.log(ourData); +// console.log("hi3") +// }; +// ourReq.send(); +// console.log("hi4") + +//JSON works individually or even in a blank dev tools but doesent log when u open index.html, why ???? +// +///////////////////////////////////////////////////////////////////////////////////////////////////////////// + +function candidatesObjToArray(candidates) { + var arrOfKeys = Object.keys(candidates); + var arrOfCandidates = arrOfKeys.map(function (key) { return candidates[key] }); + return arrOfCandidates; +} +/** + * 2 - Remove any voters who have voted for more than 2 people, or have voted for the same person twice. +*/ +function filterInvalidVoters(allVoters) { + const filteredByLength = allVoters.filter(item => item.votingCard.length < 3); + const filteredByClash = filteredByLength.filter(item => item.votingCard[0] !== item.votingCard[1]) + return filteredByClash; +} +/** + * 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. + */ +//refactored Version of runElection +function runElection(validVoters, candidates) { + // console.log(validVoters, "validVoters") + //console.log(candidates,"candidates") + for (let i = 1; i <= Object.values(candidates).length; i++) { + //console.log("candidate", i) + validVoters.forEach(function (item) { + //console.log(item); + if (item.votingCard[0] === i) { + candidates[i].numVotes += 1 + } + if (item.votingCard[1] === i) + candidates[i].numVotes += 0.5; + }) + } + //console.log("after runElec",candidates) + return candidates; +} +/** + * 4 - After an election has been run, return the winner + * + * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} + */ +function getWinner(candidates) { + let winner = { numVotes: 0.0 }; + try { + Object.values(candidates).forEach(function (item) { + /* console.log(item.name,item.numVotes); + console.log(winner.name,winner.numVotes); + console.log(); */ + if (item.numVotes == winner.numVotes) { + throw "Duplicate"; + } + else if (item.numVotes > winner.numVotes) { + winner = item; + } + }) + } catch (error) { + return null; + } + 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 mes = winner.name + " has won the election with " + winner.numVotes + " votes!"; + return mes; + } + else return "The election was a draw"; +} \ No newline at end of file From d92be3d788a148fb450b53eb1593e4675a92c07a Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Mon, 19 Mar 2018 18:49:04 +0000 Subject: [PATCH 34/38] changed endpoint --- election-part2.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/election-part2.js b/election-part2.js index 1207ac7..c969d9c 100644 --- a/election-part2.js +++ b/election-part2.js @@ -138,6 +138,7 @@ class Election { } } // Example of how the winner message can be printed. + /* fetch('http://www.mocky.io/v2/5a55224b2d000088425b1ed8') .then(function (response) { return response.json() }) @@ -165,7 +166,7 @@ runButt.addEventListener("click", () => alert(election.printWinnerMessage())); function fetchElectionData(){ var data; -fetch('https://www.mocky.io/v2/5a55224b2d000088425b1ed8').then(res => res.json()).then(function(jsonData){ data = jsonData; +fetch('https://s3-eu-west-1.amazonaws.com/lorenzomixedstuff/electionList.json').then(res => res.json()).then(function(jsonData){ data = jsonData; candidates = data.candidates; voters = data.voters; let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); From d9b4e08371c49b5fbaf2d1471088238e41f9b25e Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Tue, 20 Mar 2018 21:36:31 +0000 Subject: [PATCH 35/38] candidates and voters connected to json --- election-part2.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/election-part2.js b/election-part2.js index c969d9c..1cc2c84 100644 --- a/election-part2.js +++ b/election-part2.js @@ -28,10 +28,9 @@ function filterInvalidVoters(allVoters) { function runElection(validVoters, candidates) { // console.log(validVoters, "validVoters") //console.log(candidates,"candidates") - for (let i = 1; i <= Object.values(candidates).length; i++) { - //console.log("candidate", i) + for (let i = 0; i < candidates.length; i++) { validVoters.forEach(function (item) { - //console.log(item); + //console.log(can); if (item.votingCard[0] === i) { candidates[i].numVotes += 1 } @@ -50,7 +49,7 @@ function runElection(validVoters, candidates) { function getWinner(candidates) { let winner = { numVotes: 0.0 }; try { - Object.values(candidates).forEach(function (item) { + candidates.forEach(function (item) { /* console.log(item.name,item.numVotes); console.log(winner.name,winner.numVotes); console.log(); */ @@ -123,9 +122,9 @@ class Election { this.winner = 'winner has not been chosen yet'; } runElection() { - //console.log("runElec is getting called"); + this.candidates = runElection(this.validVoters, this.candidates) - // console.log("runElec is getting called after",this.candidates); + } getWinner() { //console.log("getwinner is getting called") @@ -162,21 +161,22 @@ listVote.forEach((item, i, listVote)=> item.textContent = votingPopulation[i].na //run Election Buttun let runButt = document.getElementById("runBut"); -runButt.addEventListener("click", () => alert(election.printWinnerMessage())); +runButt.addEventListener("click", () => alert(fetchElectionData())); function fetchElectionData(){ var data; fetch('https://s3-eu-west-1.amazonaws.com/lorenzomixedstuff/electionList.json').then(res => res.json()).then(function(jsonData){ data = jsonData; -candidates = data.candidates; +candidates = data.candidates.map(function(item){return new Candidate(item.name, item.age, item.party, item.votingCard)}); +console.log(candidates,"candddddd") voters = data.voters; -let allVoters = votingPopulation.concat(candidatesObjToArray(candidates)); +let allVoters = voters.concat(candidates); let validVoters = filterInvalidVoters(allVoters); -console.log(validVoters,"inside fetchfunction") +console.log(validVoters,"inside fetchfunction validVoters") let election = new Election(validVoters, candidates); election.runElection(); // Example of how runElection() can be called. -election.getWinner(); -console.log(candidates) +election.getWinner(); //need to modify it so it becomes a sort only selects max numVotes +console.log(candidates,"cand") console.log(election.printWinnerMessage(),"inside fetchfunction");} ) } From 1c7bf3636b313cb52060ca8deb87004a820dbaea Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Wed, 21 Mar 2018 17:56:38 +0000 Subject: [PATCH 36/38] working on new getWinner for json --- election-part2.js | 71 +++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/election-part2.js b/election-part2.js index 1cc2c84..9654bf2 100644 --- a/election-part2.js +++ b/election-part2.js @@ -48,22 +48,19 @@ function runElection(validVoters, candidates) { */ function getWinner(candidates) { let winner = { numVotes: 0.0 }; - try { - candidates.forEach(function (item) { - /* console.log(item.name,item.numVotes); - console.log(winner.name,winner.numVotes); - console.log(); */ - if (item.numVotes == winner.numVotes) { - throw "Duplicate"; - } - else if (item.numVotes > winner.numVotes) { - winner = item; - } - }) - } catch (error) { - return null; + for (let i = 0; i < candidates.length; i++) { + if (candidates[i].numVotes > winner.numVotes) { + winner = candidates[i]; + } } - return winner; + //check if two candidates have the same numVotes we have to return null + var maxDuplicate = [] + for (let i = 0; i < candidates.length; i++) { + if (candidates[i].numVotes === winner.numVotes); + maxDuplicate.push(candidates[i]) + } + if (maxDuplicate.length > 1) {return null;} + return winner; //Hila Waraich won with 26.5 votes } /** * 5 - Return a message including the name of the winner, and how many votes @@ -122,9 +119,9 @@ class Election { this.winner = 'winner has not been chosen yet'; } runElection() { - + this.candidates = runElection(this.validVoters, this.candidates) - + } getWinner() { //console.log("getwinner is getting called") @@ -136,8 +133,8 @@ class Election { return this.winner.name + " has won the election with " + this.winner.numVotes + " votes!"; } } - // Example of how the winner message can be printed. - +// Example of how the winner message can be printed. + /* fetch('http://www.mocky.io/v2/5a55224b2d000088425b1ed8') .then(function (response) { return response.json() }) @@ -157,29 +154,31 @@ listCan.forEach((item, i, listCan) => item.textContent = candidates[i + 1].name) // } //list of Voters let listVote = document.querySelectorAll(".voters li"); -listVote.forEach((item, i, listVote)=> item.textContent = votingPopulation[i].name); +listVote.forEach((item, i, listVote) => item.textContent = votingPopulation[i].name); //run Election Buttun let runButt = document.getElementById("runBut"); runButt.addEventListener("click", () => alert(fetchElectionData())); -function fetchElectionData(){ -var data; -fetch('https://s3-eu-west-1.amazonaws.com/lorenzomixedstuff/electionList.json').then(res => res.json()).then(function(jsonData){ data = jsonData; -candidates = data.candidates.map(function(item){return new Candidate(item.name, item.age, item.party, item.votingCard)}); -console.log(candidates,"candddddd") -voters = data.voters; -let allVoters = voters.concat(candidates); -let validVoters = filterInvalidVoters(allVoters); -console.log(validVoters,"inside fetchfunction validVoters") +function fetchElectionData() { + var data; + fetch('https://s3-eu-west-1.amazonaws.com/lorenzomixedstuff/electionList.json').then(res => res.json()).then(function (jsonData) { + data = jsonData; + candidates = data.candidates.map(function (item) { return new Candidate(item.name, item.age, item.party, item.votingCard) }); + console.log(candidates, "candddddd") + voters = data.voters; + let allVoters = voters.concat(candidates); + let validVoters = filterInvalidVoters(allVoters); + console.log(validVoters, "inside fetchfunction validVoters") -let election = new Election(validVoters, candidates); -election.runElection(); // Example of how runElection() can be called. -election.getWinner(); //need to modify it so it becomes a sort only selects max numVotes -console.log(candidates,"cand") -console.log(election.printWinnerMessage(),"inside fetchfunction");} -) + let election = new Election(validVoters, candidates); + election.runElection(); // Example of how runElection() can be called. + election.getWinner(); //need to modify it so it becomes a sort only selects max numVotes + console.log(election.printWinnerMessage(), "inside fetchfunction who won?"); + election.printWinnerMessage() + } + ) } fetchElectionData(); -//console.log(candidates,"last line") + From 21d7dd64c4e682f9d5f5bd8e2e8d2de1bc497474 Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Wed, 21 Mar 2018 20:03:55 +0000 Subject: [PATCH 37/38] getWinner is working now with sort method --- election-part2.js | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/election-part2.js b/election-part2.js index 9654bf2..fb59275 100644 --- a/election-part2.js +++ b/election-part2.js @@ -26,8 +26,6 @@ function filterInvalidVoters(allVoters) { */ //refactored Version of runElection function runElection(validVoters, candidates) { - // console.log(validVoters, "validVoters") - //console.log(candidates,"candidates") for (let i = 0; i < candidates.length; i++) { validVoters.forEach(function (item) { //console.log(can); @@ -38,7 +36,6 @@ function runElection(validVoters, candidates) { candidates[i].numVotes += 0.5; }) } - //console.log("after runElec",candidates) return candidates; } /** @@ -47,20 +44,10 @@ function runElection(validVoters, candidates) { * Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3} */ function getWinner(candidates) { - let winner = { numVotes: 0.0 }; - for (let i = 0; i < candidates.length; i++) { - if (candidates[i].numVotes > winner.numVotes) { - winner = candidates[i]; - } - } - //check if two candidates have the same numVotes we have to return null - var maxDuplicate = [] - for (let i = 0; i < candidates.length; i++) { - if (candidates[i].numVotes === winner.numVotes); - maxDuplicate.push(candidates[i]) - } - if (maxDuplicate.length > 1) {return null;} - return winner; //Hila Waraich won with 26.5 votes + candidates.sort(function(a,b) {return b.numVotes-a.numVotes}) + //check if two candidates have the same numVotes we have to return null-draw + if (candidates[0].numVotes===candidates[1].numVotes) {return null;} + return candidates[0]; //Hila Waraich won with 26.5 votes } /** * 5 - Return a message including the name of the winner, and how many votes From e7aa63bf6ccd134907599ab5df0cd468c1e3d66f Mon Sep 17 00:00:00 2001 From: mahsa2017 Date: Wed, 21 Mar 2018 21:53:28 +0000 Subject: [PATCH 38/38] runButt works using promise --- election-part2.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/election-part2.js b/election-part2.js index fb59275..9a26fe6 100644 --- a/election-part2.js +++ b/election-part2.js @@ -145,27 +145,25 @@ listVote.forEach((item, i, listVote) => item.textContent = votingPopulation[i].n //run Election Buttun let runButt = document.getElementById("runBut"); -runButt.addEventListener("click", () => alert(fetchElectionData())); +runButt.addEventListener("click", () => fetchElectionData().then(message=>alert(message))); function fetchElectionData() { var data; - fetch('https://s3-eu-west-1.amazonaws.com/lorenzomixedstuff/electionList.json').then(res => res.json()).then(function (jsonData) { + //return 2; + return fetch('https://s3-eu-west-1.amazonaws.com/lorenzomixedstuff/electionList.json').then(res => res.json()).then(function (jsonData) { data = jsonData; candidates = data.candidates.map(function (item) { return new Candidate(item.name, item.age, item.party, item.votingCard) }); - console.log(candidates, "candddddd") voters = data.voters; let allVoters = voters.concat(candidates); let validVoters = filterInvalidVoters(allVoters); - console.log(validVoters, "inside fetchfunction validVoters") let election = new Election(validVoters, candidates); election.runElection(); // Example of how runElection() can be called. election.getWinner(); //need to modify it so it becomes a sort only selects max numVotes - console.log(election.printWinnerMessage(), "inside fetchfunction who won?"); - election.printWinnerMessage() + var message = election.printWinnerMessage(); + return message; } ) } -fetchElectionData();