diff --git a/robin.js b/robin.js index 71d802c..8bd5c82 100644 --- a/robin.js +++ b/robin.js @@ -21,8 +21,10 @@ module.exports = function (n, ps) { // n = num players for (let i = 0; i < n / 2; i += 1) { const o = n - 1 - i; if (ps[i] !== DUMMY && ps[o] !== DUMMY) { - const iHome = i === 0 && j % 2 === 1; - rs[j].push([iHome ? ps[o] : ps[i], iHome ? ps[i] : ps[o]]); // insert pair as a match - [ away, home ] + // flip orders to ensure everyone gets roughly n/2 home matches + const isHome = i === 0 && j % 2 === 1; + // insert pair as a match - [ away, home ] + rs[j].push([isHome ? ps[o] : ps[i], isHome ? ps[i] : ps[o]]); } } ps.splice(1, 0, ps.pop()); // permutate for next round diff --git a/test/robin.test.js b/test/robin.test.js index fe31cc2..cfcc19b 100644 --- a/test/robin.test.js +++ b/test/robin.test.js @@ -5,7 +5,7 @@ var $ = require('interlude') test('robin', function *(t) { $.range(20).forEach(function (n) { var rs = robin(n); - var expected = ($.odd(n)) ? n : n-1; + var expected = $.odd(n) ? n : n-1; t.eq(expected, rs.length, 'correct number of rounds'); var pMaps = []; @@ -75,7 +75,7 @@ test('home-away', function *(t) { } } - for (let i = 1; i <= n; i++) { + for (let i = 1; i <= n; i += 1) { const isOddPlayer = i % 2 === 1; const fairAmount = (n - 1) / 2; const [ roundedUp, roundedDown ] = [ Math.ceil(fairAmount), Math.floor(fairAmount) ]; @@ -95,4 +95,4 @@ test('home-away', function *(t) { hasCorrectHomeAwayOutput(9); hasCorrectHomeAwayOutput(10); -}) \ No newline at end of file +});