Hi,

I have been creating a round robin. This is similar idea to speed dating except everyone has to talk to everyone.
Whilst I have been able to create this, there is a lot of repetition in my code and I wondering if any of you have recommendations for removing some of that, particularly around the push and pops. The code below I tested within the js.do website.
I will add the output below the code, so you get an idea of what this looks like.
Any help will be appreciated.

``````<p style="line-height: 18px; font-size: 18px;  font-family: times;">
<br>
Round robbin<br><br>
<script>
var poslow = [1,2,3,4,5,6];
var poshigh = [7,8,9,10,11,12];

// this function displays all the positions on the screen aligned
// in two rows one row has 1-6 the opposite row has 7-12
function calculate()
{
for( j=0, k=0; j < poslow.length, k < poshigh.length; j++,k++)
{
document.write('<p style="color:blue;">'+poslow[j]+" "+poshigh[k]+'</p>');

}

}

// the first round is the default, this is where everyone starts out
document.write('<p style="color:red;">'+'round 1 </p>'+"<br/>");
calculate();

// displays second round
document.write('<p style="color:red;">'+'round 2 </p>'+"<br/>");

// for the second round the 7 is taken off the begining
// of the second row and added to the beggining of the first.
// the 6 is taken off of the end of the first row and added
// to the end of the second row.

poshigh.shift();
poslow.pop();
poslow.unshift(7);
poshigh.push(6);
calculate();

// same principle for the rest of these apart from the numbers change
// according to the round and what is at the end and beginning of the rows

document.write('<p style="color:red;">'+'round 3 </p>'+"<br/>");

poshigh.shift();
poslow.pop();
poslow.unshift(8);
poshigh.push(5);
calculate();

document.write('<p style="color:red;">'+'round 4 </p>'+"<br/>");

poshigh.shift();
poslow.pop();
poslow.unshift(9);
poshigh.push(4);
calculate();

document.write('<p style="color:red;">'+'round 5 </p>'+"<br/>");

poshigh.shift();
poslow.pop();
poslow.unshift(10);
poshigh.push(3);
calculate();

document.write('<p style="color:red;">'+'round 6 </p>'+"<br/>");

poshigh.shift();
poslow.pop();
poslow.unshift(11);
poshigh.push(2);
calculate();

document.write('<p style="color:red;">'+'round 7 </p>'+"<br/>");

poshigh.shift();
poslow.pop();
poslow.unshift(12);
poshigh.push(1);
calculate();

document.write('<p style="color:red;">'+'round 8 </p>'+"<br/>");

poshigh.shift();
poslow.pop();
poslow.unshift(6);
poshigh.push(7);
calculate();

document.write('<p style="color:red;">'+'round 9 </p>'+"<br/>");

poshigh.shift();
poslow.pop();
poslow.unshift(5);
poshigh.push(8);
calculate();

document.write('<p style="color:red;">'+'round 10 </p>'+"<br/>");

poshigh.shift();
poslow.pop();
poslow.unshift(4);
poshigh.push(9);
calculate();

document.write('<p style="color:red;">'+'round 11 </p>'+"<br/>");

poshigh.shift();
poslow.pop();
poslow.unshift(3);
poshigh.push(10);
calculate();

document.write('<p style="color:red;">'+'round 12 </p>'+"<br/>");

poshigh.shift();
poslow.pop();
poslow.unshift(2);
poshigh.push(11);
calculate();

</script>
``````

Output

round 1

1 7

2 8

3 9

4 10

5 11

6 12

round 2

7 8

1 9

2 10

3 11

4 12

5 6

round 3

8 9

7 10

1 11

2 12

3 6

4 5

round 4

9 10

8 11

7 12

1 6

2 5

3 4

round 5

10 11

9 12

8 6

7 5

1 4

2 3

round 6

11 12

10 6

9 5

8 4

7 3

1 2

round 7

12 6

11 5

10 4

9 3

8 2

7 1

round 8

6 5

12 4

11 3

10 2

9 1

8 7

round 9

5 4

6 3

12 2

11 1

10 7

9 8

round 10

4 3

5 2

6 1

12 7

11 8

10 9

round 11

3 2

4 1

5 7

6 8

12 9

11 10

round 12

2 1

3 7

4 8

5 9

6 10

12 11

Thanks.

## All 3 Replies

I didn't get into the merit about the dating algorithm, but just by condensing your own logic...

``````    var poslow = [1,2,3,4,5,6];
var poshigh = [7,8,9,10,11,12];

// this function displays all the positions on the screen aligned
// in two rows one row has 1-6 the opposite row has 7-12
function calculate(round, unshift, push)
{

document.write('<p style="color:red;">'+'round ' + round + ' </p>'+"<br/>");

if ( typeof unshift != 'undefined' ) {
poshigh.shift();
poslow.pop();
poslow.unshift(unshift);
poshigh.push(push);
}

for( j=0, k=0; j < poslow.length, k < poshigh.length; j++,k++)
{
document.write('<p style="color:blue;">'+poslow[j]+" "+poshigh[k]+'</p>');
}

}

// the first round don't pass parameters of unshift so it won't change the indexes
calculate(1);

calculate(2, 7, 6);
calculate(3, 8, 5);
calculate(4, 9, 4);
calculate(5, 10, 3);
calculate(6, 11, 2);
calculate(7, 12, 1);
calculate(8, 6, 7);
calculate(9, 5, 8);
calculate(9, 4, 9);
calculate(10, 3, 10);
calculate(11, 2, 11);

</script>
``````

With further evaluation, you could get a little more dynamic...

``````    var poslow = [1,2,3,4,5,6];
var poshigh = [7,8,9,10,11,12];

var popsRounds = [
[1, undefined, undefined],
[2, 7, 6],
[3, 8, 5],
[4, 9, 4],
[5, 10, 3],
[6, 11, 2],
[7, 12, 1],
[8, 6, 7],
[9, 5, 8],
[9, 4, 9],
[10, 3, 10],
[11, 2, 11]
];

// this function displays all the positions on the screen aligned
// in two rows one row has 1-6 the opposite row has 7-12
function calculate(round, unshift, push)
{
document.write('<p style="color:red;">'+'round ' + round + ' </p>'+"<br/>");
if ( typeof unshift != 'undefined' ) {
poshigh.shift();
poslow.pop();
poslow.unshift(unshift);
poshigh.push(push);
}
for( j=0, k=0; j < poslow.length, k < poshigh.length; j++,k++)
{
document.write('<p style="color:blue;">'+poslow[j]+" "+poshigh[k]+'</p>');
}
}

for(var i=0,il=popsRounds.length;i<il;i++) {
var round = popsRounds[i];
calculate(round[0], round[1], round[2]);

// Even shorter would be
// calculate.apply(calculate, popsRounds[i]);
}
``````

Hi AleMonteiro,

Thanks for this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.