Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html><body>
 <textarea id='input'>
Red (Trump) 168: S[0] C[0] B[4] T[5]
Purply-red (Bush) 40: S[0] C[2] B[5] T[2]
Blue (Clinton) 130: S[4] C[5] B[1] T[0]
Bluer (Sanders) 30: S[5] C[4] B[1] T[0]
 </textarea>
 <textarea id='results'></textarea>
 <textarea id='blurred'></textarea>
  <div class='dialog'>
  <input type='text' id='numBlurred' value='25'></input>
 <button onclick='makeBlurred()'>make blurred</button><br><br>
 <button onclick='tabulate()'>tabulate</button>
  </div>
    <script>
    // easy ways to get and set the values of 
    // the textareas
    function getVal(id){
      return document.getElementById(id).value;
    };
    
    function setVal(id, value){
      document.getElementById(id).value = value;
    };
    
    function makeBlurred() { 
      var ballots = parseScoreBallots(getVal('input'), true);
      var candidates = getCandidateList(ballots);
      var blurredBallots = [ ];
      var numBlurredBallots = parseInt(getVal('numBlurred'));
      for(var i=0; i<numBlurredBallots; i++) {
        blurredBallots.push(getBlurredVote(ballots, (Math.random()*4)+2, 5));
      }
      
      setVal('blurred',  
        condenseBallots(blurredBallots).map(function(ballot) {
            return scoreBallotToString(ballot, candidates); 
          }).join('\n')
      );
    }  
      
   function tabulate() {
     var ballots = parseScoreBallots(getVal('input'), true);
     var candidates = getCandidateList(ballots);
     var blurred = parseScoreBallots(getVal('blurred'), true);;
     
     for(var i=0;i<blurred.length; i++) {
       ballots.push(blurred[i]);
     }
     
     setVal('results',  
       "----- processed " + ballots.length + " ballots -----\n" +
       " (" + (ballots.length-blurred.length) + " explicit and "  + 
        blurred.length + " blurred)\n" +

       "\n----- Pairwise wins -----\n" + 
       tabulateCondorcet(ballots, candidates).map(function(score){
          return score.name + ': ' + score.score;  
        }).join('\n') + 
            
       "\n----- Score -----\n" + 
       tabulateScore(ballots, candidates).map(function(score){
          return score.name + ': ' + score.score + ' (' + (score.score / ballots.length) + ')';  
        }).join('\n') + 
            
       "\n----- Interpolated Median -----\n" + 
       tabulateIMedian(ballots, candidates).map(function(score){
          return score.name + ': ' + score.score;  
        }).join('\n') + 
            
       "\n----- STAR -----\n" + 
       tabulateStar(ballots, candidates).round2.map(function(score){
          return score.name + ': ' + score.score;  
        }).join('\n') +        
       
       "\n----- STLR -----\n" + 
       tabulateStlr(ballots, candidates, 5).round2.map(function(score){
          return score.name + ': ' + score.score;  
        }).join('\n')       
       
       
       
     );
   }
  </script>
</body></html>
              
            
!

CSS

              
                input {
  background-color: white;
  color: black;
  width: 60px;
  display: inline-block;
}
button {
  width: 120px;
  display: inline-block;
}
.dialog {
  position: absolute;
  bottom: 20px;
  right: 30px;
  vertical-align: top;
  text-align: right;
  width: 100px;
}
textarea {
  width: 25%;
  background-color: black;
  color: #0f0;
  font-size: 15px;
  padding: 3px;
  box-shadow: 2px 2px 4px #000;
}

              
            
!

JS

              
                function getBlurredVote (list, num, maxValue) {
  var v, out = {};
  
  while(true) {
    var min = 999999999, max = 0;
    num = num || 1;

    for(var j=0; j<num; j++) {
      v = getRandomVote(list);
      for(var i in v) {
        out[i] = (out[i]||0) + v[i];
      }
    }
    for(var i in v) {
      if(out[i] < min) {
        min = out[i]
      } 
      if(out[i] > max) {
        max = out[i]
      } 
    }

    if(min != max) {  
      for(var i in v) {
        out[i] = Math.round((out[i]-min) * (maxValue/(max-min)));
      }
    return out;
    }
  }
}


function scoreBallotToSimpleString(ballot, candidates) {
  return candidates.map(function(c){
    return ballot[c] || 0;
  }).join(',');
}

function compareTwoCandidates (c1, c2, ballots) {
  var count1 = 0, count2 = 0, ties = 0;
  for(var i=0; i<ballots.length; i++) {
    var diff = ballots[i][c2] -  ballots[i][c1];
    if(diff > 0) {
      count2++;
    } else if (diff < 0){
      count1++;
    } else {
      ties++;
    }
  }
  return [count1, count2, ties];
}

function tabulateCondorcet(ballots, candidates) {
  var scores = [];
  for(var j=0; j<candidates.length; j++) {
    scores[j] = {
      name: candidates[j],
      score: 0
    };
  }
  for(var j=0; j<candidates.length; j++) {
    for(var i=0; i<candidates.length; i++) {
      var res = compareTwoCandidates(candidates[i], candidates[j], ballots);
      if (res[1] > res[0]) {
        scores[j].score += 1; 
      }
    }
  }
  scores.sort(function(a, b){
    return b.score - a.score;    
  });
  return scores;
}

function tabulateScore(ballots, candidates) {
  var scores = [];
  for(var j=0; j<candidates.length; j++) {
    scores[j] = {
      name: candidates[j],
      score: 0
    };
  }
  for(var i=0; i<ballots.length; i++) {
    var ballot = ballots[i];
    for(var j=0; j<candidates.length; j++) {
      scores[j].score += ballot[candidates[j]] || 0;    
    }
  }
  /*for(var i=0; i<scores.length; i++) {
    scores[i].score /= ballots.length;
  } */
  scores.sort(function(a, b){
    return b.score - a.score;    
  });
  return scores;
}

function tabulateStar(ballots, candidates) {
  var scores = tabulateScore(ballots, candidates);
  var s1 = scores[0], s2 = scores[1];
  var v1 = 0, v2 = 0;
  for(var i=0; i<ballots.length; i++) {
     var r1 = ballots[i][s1.name] || 0;  
     var r2 = ballots[i][s2.name] || 0;  
     if(r1 > r2) {
       v1++;
     } else if(r2 > r1) {
       v2++;
     }

  }
  var round2 = [
      {
       name: s1.name,
       score: v1
      },
      {
       name: s2.name,
       score: v2
      }
    ];
  round2.sort(function(a, b){
    return b.s - a.s;    
  });
  return {
    round1: scores,
    round2: round2
  };  
}

function tabulateStlr(ballots, candidates, maxVal) {
  var scores = tabulateScore(ballots, candidates);
  var s1 = scores[0], s2 = scores[1];
  var v1 = 0, v2 = 0;
  for(var i=0; i<ballots.length; i++) {
    var r1 = ballots[i][s1.name] || 0;  
    var r2 = ballots[i][s2.name] || 0;  
    if(r1 > 0 || r2 > 0) {
      var scaler = maxVal / ((r1>r2)?r1:r2);
      v1 += r1 * scaler;    
      v2 += r2 * scaler;
     }
  }
  var round2 = [
      {
       name: s1.name,
       score: v1
      },
      {
       name: s2.name,
       score: v2
      }
    ];
  round2.sort(function(a, b){
    return b.score - a.score;    
  });
  return {
    round1: scores,
    round2: round2
  };  
}

function tabulateIMedian(ballots, candidates) {
  var scores = [];
  for(var j=0; j<candidates.length; j++) {
    scores[j] = {
      name: candidates[j],
      sc: new ScoreCollection(5)
    };
  }
  for(var i=0; i<ballots.length; i++) {
    var ballot = ballots[i];
    for(var j=0; j<candidates.length; j++) {
      scores[j].sc.addScore (ballot[candidates[j]] || 0);
    }
  }
  for(var i=0; i<scores.length; i++) {
    scores[i].score = scores[i].sc.getMedian(true);
  }
  scores.sort(function(a, b){
    return b.score - a.score;    
  });
  return scores;
}


// for median
class ScoreCollection {
  
  constructor (max) {
    this.max = max;
    this.counts = [];
    for(var i=0;i<max+1; i++) {
      this.counts.push(0);
    }
    this.numScores = 0;
  }
  
  addScore(which) {
    which = Math.round(which);
    if(which < 0) {
      which = 0;
    }
    if(which > this.max) {
      which = this.max;
    }
    this.counts[which]++;
    this.numScores++;
  }
  
  getMedian(interpolate){
    var counts = this.counts;
    
    var half = this.numScores / 2;
    var min = 0, max;
    for(var i=0; i<this.counts.length; i++) {
      max = min + this.counts[i];
      if(min < half && half <= max) {
        if(interpolate) {
          return i + ((half-min)/(max-min)) - .5;
        } else {
          return i + ((max==half)?.5:0);
        }
      }
      min = max;
    }
    return null;
  }
}

// shouldn't be necessary? (it in an included
// codepen but getting it from cache)
function getCandidateList (ballotList) {
  var candidateArray = [], candidateHash = {};
  for(var i=0; i<ballotList.length; i++) {
    var vote = ballotList[i];
    for(var j in vote) {
      if(j != 'count') {
        if(!candidateHash[j]) {
          candidateArray.push(j);
          candidateHash[j] = true;         
        }  
      }
    }    
  }
  candidateArray.sort();
  return candidateArray;
}

function condenseBallots(ballots) {
  var hash = {};
  
  for(var i=0; i<ballots.length; i++) {
    var s = scoreBallotToString(ballots[i], 5);
    if(hash[s]) {
      hash[s]++;
    } else {
      hash[s] = 1;
    }
  }
  var array = [];
  for(var i in hash) {
    var b = parseScoreBallot(i);
    b.count = hash[i];
    array.push(b);
  }
  array.sort(function(a,b) {
      return b.count - a.count;
    });
  
  return array;
}

// parse many of them
function parseScoreBallots  (string, expand) {  
     var a = string.split('\n');
     var ballots = [ ];
     for(var i=0; i<a.length; i++) {
       var b = parseScoreBallot(a[i]);
       if(b) {
         ballots.push(b);
       }
     }
     return (expand) ? getExpandedList(ballots) : ballots;
   }

              
            
!
999px

Console