Last active
April 5, 2016 21:56
-
-
Save raymondflores/1972c013038cbe2473495a0d9f678fa6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function optimize () { | |
var budget = 50000; | |
var current_salary = 0; | |
var team = []; | |
var constraints = { | |
'QB': 1, | |
'RB': 2, | |
'WR': 3, | |
'TE': 1, | |
'DST': 1, | |
'FLEX': 1 | |
}; | |
var counts = { | |
'QB': 0, | |
'RB': 0, | |
'WR': 0, | |
'TE': 0, | |
'DST': 0, | |
'FLEX': 0 | |
}; | |
for (var i in $rootScope.players) { | |
var pos = $rootScope.players[i].position; | |
var sal = $rootScope.players[i].salary; | |
if (counts[pos] < constraints[pos] && current_salary + sal <= budget) { | |
team.push($rootScope.players[i]); | |
counts[pos]++; | |
current_salary += sal; | |
} | |
else if (counts['FLEX'] < constraints['FLEX'] && current_salary + sal <= budget && ['RB','WR','TE'].indexOf(pos) !== -1) { | |
team.push($rootScope.players[i]); | |
counts['FLEX']++; | |
current_salary += sal; | |
} | |
} | |
$rootScope.players.sort( function (a, b) { | |
return b.dk_total - a.dk_total; | |
}); | |
for (var j in $rootScope.players) { | |
var pos = $rootScope.players[j].position; | |
var sal = $rootScope.players[j].salary; | |
var pts = $rootScope.players[j].dk_total; | |
if (team.indexOf($rootScope.players[j]) === -1) { | |
var pos_players = team.filter( function (player) { | |
return player.position === pos; | |
}) | |
pos_players.sort( function (a, b) { | |
return a.dk_total - b.dk_total; | |
}); | |
for (var k in pos_players) { | |
if (((current_salary + sal - pos_players[k].salary) <= budget) && (pts > pos_players[k].dk_total)) { | |
team[team.indexOf(pos_players[k])] = $rootScope.players[j]; | |
current_salary = current_salary + sal - pos_players[k].salary; | |
break; | |
} | |
} | |
} | |
} | |
return team; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment