JSFiddle - React, Tailwind, and code Playground

by Jonathon Mascorella

HTML

<script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">
<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->

<div class="container-fluid">
  <div class="row">
    <div class="col mt-3">
      <h1>
        Voting script
      </h1>
    </div>
  </div>
  <div class="row">
    <div class="col">
      <div class="form-group">
        <button class="btn btn-warning" id="calculateVotes">
          Calculate Votes
        </button>
      </div>
    </div>
  </div>

  <div class="row mt-1">
    <div class="col">
      <div class="form-group">
        <textarea name="ballots" class="form-control" id="ballots" rows=10></textarea>
        <p>
          <span>Status: </span><span id="status">Waiting..</span>
          <span>Total votes to count:</span><span id="totalVotesCounted"></span>
        </p>
      </div>
    </div>
  </div>
  <div class="row mb-5" id="result">

  </div>
</div>

JavaScript

Ballots = [];
weightedBallots = [];
resolvedBallots = [];
Rounds = [];

candidates = ["Mickey", "Minnie", "Donald", "Snoopy", "Fred", "George", "Daisy", "Duck", "Goofy"];

$("#calculateVotes").click(function() {

  $("#status").text("Let's do it!");
  text = $("#ballots").val()
  Ballots = CSVToArray(text);

  $("#status").text("Resolving weightings...");
  
  for (i = 0; i < Ballots.length; i++) {
    w = Ballots[i][Ballots[i].length-1];
    Ballots[i].pop();
    //Add that many items of the same item to the array
    for(a = 0; a < w; a++) {
    	temp = [];
      $.each(Ballots[i], function(v){
        temp.push(Ballots[i][v]);
      });
  		weightedBallots.push(temp);
      $("#totalVotesCounted").val(weightedBallots.length);
  	}
  }
  
  
  $("#status").text("Weightings resolved...");
  //////

  numberOfRounds = candidates.length;
  for (r = 0; r < numberOfRounds; r++) {
    $("#status").text("Counting votes for Round " + r);
    Rounds.push([]);
 		
    lowestVoteThisRound = 0;
    lowestCandiate = ""
    candidate = "";
    candidateVoteCount = 0;
    highestVote = 0;
    winningCandidate = "";
    
    for (a = 0; a < candidates.length; a++) {
      candidate = candidates[a];
      candidateVoteCount = 0;
      
      for (i = 0; i < weightedBallots.length; i++) {
        if (weightedBallots[i][0] == candidate) {
          //Count the ballot
          candidateVoteCount++;
        }
      }
      //console.log(candidate, candidateVoteCount);
      
      currentRound = [];
      currentRound.push(candidate);
    	currentRound.push(candidateVoteCount);
      Rounds[r][a] = currentRound;
      // Count the lowest vote for the round
      
      if(a == 0 && candidateVoteCount > 0){
        lowestVoteThisRound = candidateVoteCount;
        lowestCandidate = candidate;
        
        if(highestVote < candidateVoteCount){
          highestVote = candidateVoteCount;
          winningCandidate = candidate;
        }
        
      }else if (a > 0 &&...