Skip to content

Commit

Permalink
Add logic to show more winners when having same votes
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshs committed Oct 5, 2024
1 parent a0aaad0 commit d3733cf
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ <h3 class="text-center">Enter the Votes for each Candidate:</h3>
<div id="results" class="mt-4" style="display:none;">
<div class="card p-3">
<h2 class="text-center">Results:</h2>
<p><strong>Winner:</strong> <span id="winner"></span></p>
<p><strong>Winner(s):</strong> <span id="winner"></span></p>
<p><strong>Total Votes:</strong> <span id="totalVotes"></span></p>

<h3 class="text-center">Vote Counts:</h3>
Expand Down Expand Up @@ -116,7 +116,7 @@ <h3 class="text-center">Vote Counts:</h3>
parseInt(document.getElementById("candidate3").value),
parseInt(document.getElementById("candidate4").value)
];

// Send votes to the server
fetch("/vote", {
method: "POST",
Expand All @@ -130,7 +130,16 @@ <h3 class="text-center">Vote Counts:</h3>
document.getElementById("loading").style.display = "none";
document.getElementById("results").style.display = "block";

document.getElementById("winner").innerText = "Candidate " + (data.winner + 1);
// Handling joint winners in case of ties
const maxVotes = Math.max(...votes);
const winners = [];
for (let i = 0; i < votes.length; i++) {
if (votes[i] === maxVotes) {
winners.push("Candidate " + (i + 1));
}
}

document.getElementById("winner").innerText = winners.join(", ");
document.getElementById("totalVotes").innerText = data.votes + " votes";
document.getElementById("histogram").src = "data:image/png;base64," + data.image;
});
Expand Down

0 comments on commit d3733cf

Please sign in to comment.