JSFiddle - React, Tailwind, and code Playground
by sstur
HTML
<textarea>
QB Tannehill Ryan $8,300
QB Matthew Stafford $7,800
QB Phillip Rivers $8,400
RB Arian Foster $8,600
RB Fred Jackson $6,900
RB Mark Ingram $7,300
WR Charles Johnson $6,100
WR Mike Evans $7,600
WR Steve Smith $6,300
TE Heath Miller $5,300
TE Greg Olsen $6,400
TE Jordan Cameron $5,100
K Cody Parkey $5,100
K Justin Tucker $4,800
K Phil Dawson $4,600
D Ravens $5,200
D Eagles $5,000
D Chargers $4,800
</textarea>
<button>Make Team</button>
<pre><code></code></pre>
CSS
textarea {
display: block;
width: 100%;
height: 100px;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
button {
margin: 8px 0;
}
JavaScript
//var f = function() {/**/}
//var m = f.toString().match(/\/\*([\s\S]*)\*\//)
//var s = m && m[1] || '';
var COUNTS = {
QB: 1,
RB: 2,
WR: 3,
TE: 1,
K: 1,
D: 1
};
var MAX_VALUE = 60000;
var textarea = document.querySelector('textarea');
var resultPane = document.querySelector('code');
document.querySelector('button').onclick = function() {
var result = getPermutation();
var players = result.items.map(renderRow).join('\n');
resultPane.innerText = players + '\n' + 'total: ' + cur(result.total);
};
function getPermutation() {
var timer = Date.now();
var positions = parsePlayers();
while (teamValue == null || teamValue > MAX_VALUE) {
var items = [], teamValue = 0;
Object.keys(positions).forEach(function(position) {
var list = positions[position];
var chooseCount = COUNTS[position];
if (list.length <= chooseCount) {
var chosen = list;
} else {
list = list.slice();
chosen = [];
while (chosen.length < chooseCount && list.length) {
console.log(chosen.length, chooseCount, list.length);
var i = Math.floor(Math.random() * list.length);
chosen.push(list[i]);
list.splice(i, 1);
}
}
items.push.apply(items, chosen);
chosen.forEach(function(item) {
teamValue += item.value;
});
});
if (Date.now() - timer > 1000) break;
}
return {items: items, total: teamValue};
}
function parsePlayers() {
var positions = {};
var list = textarea.value.trim().split(/\r\n|\r|\n/);
list.forEach(function(line) {
var cells = line.split('\t');
var position = cells[0].toUpperCase();
var player = cells[1];
var value = parseInt(cells[2].replace(/[^\d]/g, ''), 10);
var arr = positions[position] ||...