Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
JS Round Robin Cyclic Algorithm with array

I try to apply the algorithm from this source to array like this:

function tournament (n) {
  let pairs = [];
  const k = n.length

  for (let r=1; r<k; r++) {
    for (let i=1; i <= k/2; i++) {
      if (i==1) {
        pairs.push([n[0], n[(k-1+r-1) % (k-1) + 2]]);
      } else
      pairs.push([n[(r+i-2) % (k-1) + 2], n[(k-1+r-i) % (k-1) + 2]]);
    }
  }
  
  return pairs
}

console.log(JSON.stringify(tournament([1,2,3,4,5,6]), null, 2))

But some of pairs has null value in output. How can I fix it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The answer is subtract 1 from each expression:

function tournament (n) {
  let pairs = [];
  const k = n.length

  for (let r=1; r<k; r++) {
    for (let i=1; i <= k/2; i++) {
      if (i==1) {
        pairs.push([n[1-1], n[(k-1+r-1) % (k-1) + 1]]);
      } else
      pairs.push([n[(r+i-2) % (k-1) + 1], n[(k-1+r-i) % (k-1) + 1]]);
    }
  }
  
  return pairs
}

console.log(JSON.stringify(tournament([1,2,3,4,5,6]), null, 2))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!