0

So I'm creating a Fantasy Football draft helper, and it's a 12-team snake draft format, which proceeds 1 through 12 LTR, then 13 through 24 RTL, then 25 through 36 LTR

----->

<-----

------>

So, the person with the #1 overall position (Team A) also finds themselves in the #24 and #25 position.

The #12 position also has the #13 position, as the order "wraps around" a grid.

Like so

Here is what I have come up with to flag any team's overall orders dynamically, based on draft position, but I'm no math whiz, and it seems wonky to have a special case for position=1 that doesn't apply to any of the other cases. This works, but I want to know what the real math is for this, if anyone knows.

Here is a fiddle I created with some sample code, based on my 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; }
for (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 for first position ?
    if (POS==1 && (i % (2*TEAMS)) == 0) {
        isMyTurn = true;
    }
    var turnText = (isMyTurn) ? i + " My Turn!" : i ;
    $overallOrder.append('<li>'+turnText+'</li>');    
}

Try changing the POS var from 1-12 and you can see the results. Again, I'm looking for the mathematical function that would encapsulate the above logic, if anyone knows. Thanks!

http://jsfiddle.net/3oy1w71c/1/

Thanks!

1 Answer 1

0

Comments in your code would be nice.

The fundamental problem is that % runs from 0 to 2*TEAMS-1, but you’re thinking from 1 to 2*TEAMS: you end up needing a special case for POS==1 is because (2*TEAMS)-(POS-1) turns out to be 2*TEAMS, but i%(2*TEAMS) is never 2*TEAMS. The easy fix is to change it to ((2*TEAMS)-(POS-1))%(2*TEAMS). This allows you to get things down to

var turnText = i;

if ((i % (2*TEAMS)) == ((2*TEAMS)-(POS-1))%(2*TEAMS) 
    || (i % (2*TEAMS)) == POS) {
    turnText += " My Turn!";
}

Another approach is to use (i-1)%(2*TEAMS)+1 to get the modulus into 1 to 2*TEAMS.

If you want to get it down to one conditional not using ||, you can exploit the symmetry of the snake around TEAMS+.5.

if ( Math.abs(TEAMS+.5-((i-1)%(2*TEAMS)+1))==TEAMS+.5-POS )

But I find this harder to read than the previous version.

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.