JSFiddle - React, Tailwind, and code Playground

by cjblomqvist

JavaScript

/* 
 * 
 * Undvik att processa samma combinedHappyHash två gånger
 * 
 * 
 * 
 */

var _ = require('underscore')
    ;

var totalVotes = 500
  , maxCats = 100 // Max: 100
  , maxDogs = maxCats
  , processed = {}
  , input
  , minRelated = 20 // Max realDesiredLength
  , maxRelated = minRelated + 5
  , realDesiredLength = minRelated * 2
  , related
  , testInput = {}
  , runs = 0
  , runs2 = 0
  , runs3 = 0
  , runs4 = 0
    ;

var generateData = function () {
  var input
    , existing = {}
    , desiredLength = realDesiredLength
    , votes
    , cat
    , vote
    , loop = totalVotes
    , highest = 0
      ;
  
  cat = (_.random(1, 2) === 2 ? true : false);
  vote = [(cat ? 'c' : 'd') + _.random(0, maxCats - 1), (cat ? 'd' : 'c') + _.random(0, maxCats - 1)];
  existing[vote[0] + vote[1]] = true;
  input = [[[(cat ? 'c' : 'd') + _.random(0, maxCats - 1), (cat ? 'd' : 'c') + _.random(0, maxCats - 1)], loop]];

  for(var i = 1; i < desiredLength; ) {
    votes = _.random(1, input[highest][1] - 2);
    cat = (_.random(1, 2) === 2 ? true : false);
    vote = [(cat ? 'c' : 'd') + _.random(0, maxCats - 1), (cat ? 'd' : 'c') + _.random(0, maxCats - 1)];

    if(!existing.hasOwnProperty(vote[0] + vote[1])) {
      existing[vote[0] + vote[1]] = true;
      desiredLength--;

      input[highest][1] -= votes;
      input.push([vote, votes]);
      highest = 0;
      for(var j = 1; j < input.length; j++) {
        if(input[highest][1] < input[j][1]) {
          highest = j;
        }
      }
    }
  }

  return input;
};

var success = false;

for(var tempi = 0; tempi < 10000; tempi++) {
  input = generateData();

  related = 0;

  for(var tempi2 = 0; tempi2 < input.length; tempi2++) {
    for(var tempi3 = 0; tempi3 < input.length; tempi3++) {
      if(tempi2 !== tempi3) {
        if(input[tempi2][0][0] === input[tempi3][0][1] || input[tempi2][0][1] === input[tempi3][0][0]) {
          related++;
          break;
        }
      }
    }
  }

  if(related >= minRelated &&...