Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
5edf5f1
first exercise done
mahsa2017 Feb 20, 2018
38497d0
second exercise done
mahsa2017 Feb 20, 2018
4fd3ab1
third exercise done
mahsa2017 Feb 20, 2018
b1b62f1
fourth exercise done
mahsa2017 Feb 20, 2018
29531c5
fifth exercise done
mahsa2017 Feb 20, 2018
020bf76
fifth exercise added sth
mahsa2017 Feb 20, 2018
18a20f0
fifth exercise done and code cleaned a bit
mahsa2017 Feb 20, 2018
4182ca5
sixth exercise done and fourth exercise changed totally logically
mahsa2017 Feb 20, 2018
7404690
fourth exercise modified a bit
mahsa2017 Feb 23, 2018
0b40a7f
fourth exercise modified a bit
mahsa2017 Feb 23, 2018
fc0af22
nour aldin vote changed to 4,1 like it was before sixth exercise
mahsa2017 Feb 23, 2018
f165f6a
the message for winner edited
mahsa2017 Feb 23, 2018
9ebd566
part2 first exer done
mahsa2017 Feb 26, 2018
3b73e1f
part2 second exer done
mahsa2017 Feb 26, 2018
73d8033
part2 second exer done
mahsa2017 Feb 26, 2018
e6d7b1b
part2 second exer done
mahsa2017 Feb 26, 2018
625f110
part2 second exer done
mahsa2017 Feb 26, 2018
456f3f0
part2 second exer done
mahsa2017 Feb 27, 2018
6cf8133
second exer modification of part 2
mahsa2017 Feb 27, 2018
a694df7
second exer modification of part 2
mahsa2017 Feb 27, 2018
36a877a
second exer modification of part 2
mahsa2017 Feb 27, 2018
3d0ae7b
still modifying part 2 exer 2
mahsa2017 Feb 27, 2018
fd12b9d
part2 almost done and part 1 refactored
mahsa2017 Mar 3, 2018
7766b82
last changes
mahsa2017 Mar 6, 2018
3771e6c
part 2 still has to do
mahsa2017 Mar 9, 2018
ca7ee7c
i added run election to my part2
mahsa2017 Mar 10, 2018
d31117f
i changed the order of class variables in candidate class at line 32
mahsa2017 Mar 10, 2018
5a86a68
button and static lists of cand and voters using dom provided
mahsa2017 Mar 13, 2018
5daab02
debug commit to check what changed
mahsa2017 Mar 13, 2018
f166fe5
debug commit to check what changed
mahsa2017 Mar 13, 2018
e0bd615
issue if i add an li tag to cand list
mahsa2017 Mar 13, 2018
cca27d6
xmlhttprequest provided but not logs data yet
mahsa2017 Mar 14, 2018
e962518
tried working with json
mahsa2017 Mar 19, 2018
d92be3d
changed endpoint
mahsa2017 Mar 19, 2018
d9b4e08
candidates and voters connected to json
mahsa2017 Mar 20, 2018
1c7bf36
working on new getWinner for json
mahsa2017 Mar 21, 2018
21d7dd6
getWinner is working now with sort method
mahsa2017 Mar 21, 2018
e7aa63b
runButt works using promise
mahsa2017 Mar 21, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"editor.minimap.enabled": false
}
186 changes: 156 additions & 30 deletions election-part2.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,169 @@
// Importing the functions from what you did in part 1.
const {
candidatesObjToArray,
filterInvalidVoters,
runElection,
getWinner,
winnerMessage,
} = require('./election');

// const {
// candidatesObjToArray,
// filterInvalidVoters,
// runElection,
// getWinner,
// winnerMessage,
// } = require('./election');
function candidatesObjToArray(candidates) {
var arrOfKeys = Object.keys(candidates);
var arrOfCandidates = arrOfKeys.map(function (key) { return candidates[key] });
return arrOfCandidates;
}
/**
* 1 - Write a Voter class modelling a member of the population who votes in the election.
* 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) {
for (let i = 0; i < candidates.length; i++) {
validVoters.forEach(function (item) {
//console.log(can);
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
*
* Desired return value: {name: "Tamara Faiza", age: 46, party: "Pizza Party", numVotes: 3}
*/
function getWinner(candidates) {
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
* 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;
this.age = age;
this.votingCard = votingCard;
}
}
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])
]
/**
* 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, party, votingCard) {
super(name, age, votingCard);
this.party = party;
this.numVotes = 0;
}
}
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;
//console.log(validVoters, "validVoters in class");
this.candidates = candidates;
this.winner = 'winner has not been chosen yet';
}
runElection() {

this.candidates = runElection(this.validVoters, 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!";
}
}
// 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); }) */

//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); //?????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;
// }
//list of Voters
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", () => fetchElectionData().then(message=>alert(message)));

function fetchElectionData() {
var data;
//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) });
voters = data.voters;
let allVoters = voters.concat(candidates);
let validVoters = filterInvalidVoters(allVoters);

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
var message = election.printWinnerMessage();
return message;
}
)
}



// Include your votingPopulation array here.
let votingPopulation = [];


// Include your candidates object here.
let candidates = {};


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.

console.log(election.printWinnerMessage()); // Example of how the winner message can be printed.
86 changes: 62 additions & 24 deletions election.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,110 @@
* 1 - Convert candidates object to array
*/
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(voters) {

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.
*/
function runElection(voters, 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
*
* 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";
}

// 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 }
} //4,3 is draw

let allVoters = votingPopulation.concat(candidatesObjToArray(candidates));
//console.log(allVoters);

let validVoters = filterInvalidVoters(allVoters);
//console.log(validVoters,"validdddddddddd") //--> array of Objects

candidates = runElection(validVoters, candidates);
//console.log(candidates);

let winner = getWinner(candidates);
//console.log("and the winner is: ",winner)

console.log(winnerMessage());

module.exports = {
candidatesObjToArray,
filterInvalidVoters,
runElection,
getWinner,
winnerMessage
candidatesObjToArray,
filterInvalidVoters,
runElection,
getWinner,
winnerMessage
}

10 changes: 10 additions & 0 deletions election.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
20 changes: 20 additions & 0 deletions electionclass.js
Original file line number Diff line number Diff line change
@@ -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
}
Loading