javascript - Snake Draft - Overall Order Position (Math Related) -
so i'm creating fantasy football draft helper, , it's 12-team snake draft format, proceeds 1 through 12 ltr, 13 through 24 rtl, 25 through 36 ltr
----->
<-----
------>
so, person #1 overall position (team a) finds in #24 , #25 position.
the #12 position has #13 position, order "wraps around" grid.
here have come flag team's overall orders dynamically, based on draft position, i'm no math whiz, , seems wonky have special case position=1 doesn't apply of other cases. works, want know real math this, if knows.
here fiddle created sample code, based on current solution.
var teams = 12; var rounds = 4; var pos = 1; var $overallorder = $('#overallorder'); var total = teams * rounds; if (pos > teams) { alert("only " + teams + " teams"); return false; } (var i=1; i<=total; i++) { var ismyturn = false; if ((i % (2*teams)) == ((2*teams) - (pos-1))) { ismyturn = true; } if ((i % (2*teams)) == pos) { ismyturn = true; } //special case first position ? if (pos==1 && (i % (2*teams)) == 0) { ismyturn = true; } var turntext = (ismyturn) ? + " turn!" : ; $overallorder.append('<li>'+turntext+'</li>'); } try changing pos var 1-12 , can see results. again, i'm looking mathematical function encapsulate above logic, if knows. thanks!
http://jsfiddle.net/3oy1w71c/1/
thanks!
comments in code nice.
the fundamental problem % runs 0 2*teams-1, you’re thinking 1 2*teams: end needing special case pos==1 because (2*teams)-(pos-1) turns out 2*teams, i%(2*teams) never 2*teams. easy fix change ((2*teams)-(pos-1))%(2*teams). allows things down to
var turntext = i; if ((i % (2*teams)) == ((2*teams)-(pos-1))%(2*teams) || (i % (2*teams)) == pos) { turntext += " turn!"; } another approach use (i-1)%(2*teams)+1 modulus 1 2*teams.
if want down 1 conditional not using ||, can exploit symmetry of snake around teams+.5.
if ( math.abs(teams+.5-((i-1)%(2*teams)+1))==teams+.5-pos ) but find harder read previous version.
Comments
Post a Comment