Created
May 23, 2023 22:56
-
-
Save gioiliop7/8ab08fa7ee8d931185a734c82ff17d9b to your computer and use it in GitHub Desktop.
Calculate parliament seats with "Enisxymeni analogiki" system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calculateSeats(percentages) { | |
const totalSeats = 300; // Total number of seats | |
const threshold = 3; // Percentage threshold for proportional allocation | |
const totalVotes = percentages.reduce((sum, percentage) => sum + percentage, 0); | |
const allocatedSeats = []; | |
// Calculate seats for proportional allocation | |
const proportionalSeats = totalSeats - 50; // Remaining seats after allocating 50 for the first party | |
for (let i = 0; i < percentages.length; i++) { | |
if (percentages[i] >= threshold) { | |
const seats = Math.round((percentages[i] / totalVotes) * proportionalSeats); | |
allocatedSeats.push(seats); | |
} else { | |
allocatedSeats.push(0); | |
} | |
} | |
// Add seats for the first party (plurality winner) | |
const remainingSeats = totalSeats - allocatedSeats.reduce((sum, seats) => sum + seats, 0); | |
allocatedSeats[0] += remainingSeats; | |
return allocatedSeats; | |
} | |
// Example usage: | |
const percentages = [40.79, 20.07, 11.45, 7.23, 4.45]; // Percentages for each party | |
const result = calculateSeats(percentages); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment