1

I'm creating a table seating function in javascript, directly below, for a scenario where there is a population of P participants, in this case tested with 80, and S seats per table, in this case tested by 8, each participant may only visit each table once with a total of ten tables and each participant may not meet another participant more than once for a minimum of 10 rotations. How can I make 10 unique sets of P/S, 10 times?

To explain my naming, arrayArray is the array for each of the tables and the outer arrays inner array elements are the table seating, arrayArrayPrevious is the list of everyone whose already been to a table, and the participantArray is an array of all possible participants.

The trouble seems to be when finding two participants have already met and moving the second one to the end of the participantArray to be tried again later results in only one participant ever being placed.

I'm placing the entire code below the function snippet in the event someone can help solve it and its useful for others in the future.

   function notMetAlready(w, arrayArray, participantAlreadyMet){
       if(arrayArray.includes(participantAlreadyMet)){
            var moveToEnd = arrayArray[w][0];
           console.log(moveToEnd);
            participantArray.push(moveToEnd);
           console.log(participantArray);
            return true;
        //console.log(index);   
          }
        if(!arrayArray.includes(participantAlreadyMet)){
            return true;
        }
       else{
           return false;
       }
   }

Full Code

<html>
    <head>
<script type="text/javascript">
    
    var tables = 10;
    var participants = 80;
    var participantPool = [];
    var seatingPerTable = participants/tables;
    var arrayArray = new Array(tables);
    
    for(var i = 1; i <= participants; i++){
        participantPool.push(i);
    }
        var count = 1;

    var participantArray = new Array(participants);
    var participantAlreadyMet = new Array(participants);
    var arrayArrayPrevious = new Array(tables);
    
    for (var i = 0; i <= tables; i++) { 
        arrayArrayPrevious[i] = []; 
        arrayArray[i] = []; 
    } 
    
    
    for (var i = i; i < participantAlreadyMet.length; i++) { 
         participantAlreadyMet[i] = [i];
        
    }    

function MatchingPairs() {
        
    for (var i = 0; i < tables; i++) { 
        arrayArray[i] = []; 
    } 
    
    for(var  h = 0; h < participants; h++){
            participantArray[i] = i+1;
    }
    
                for(var w = 0; w < arrayArray.length; w++){
                    if(tablesHaveNotIncluded(w,0)){
                       // for(var n = 1; n < participants; n++){
                        
                        do{
                            if(tablesDoNotInclude(0)  && notMetAlready(w, arrayArray[w], participantAlreadyMet[0])){
                                arrayArray[w].push(participantArray[0]);
                                arrayArrayPrevious[w].push(participantArray[0]);
                                participantArray.shift();
                            }
                        
                        }while(participantArray >= 0);
            }
        }
   function notMetAlready(w, arrayArray, participantAlreadyMet){
       if(arrayArray.includes(participantAlreadyMet)){
            var moveToEnd = arrayArray[w][0];
           console.log(moveToEnd);
            participantArray.push(moveToEnd);
           console.log(participantArray);
            return true;
          }
        if(!arrayArray.includes(participantAlreadyMet)){
            return true;
        }
       else{
           return false;
       }
   }
                            
    
    for(var z = 0; z < tables; z++){
        var plus = z + 1;
        console.log("Table " + plus + " " + arrayArray[z] );
    }

                        console.log("Rotation " + count);
                        count++;
            
    function tablesHaveNotIncluded(w,n){
            var outerArray = arrayArrayPrevious[w];
            if(!outerArray.includes(n)){
                    return true;
                }
            return false;
        }
    
        function tablesDoNotInclude(n){
            for(var w = 0; w < tables; w++){
                if(!arrayArray[w].includes(n)){
                    return true;
                }
            }
            return false;
        }
    }   
            

</script>
    </head>

<body>

<button onclick="MatchingPairs()">Combinations</button>

</body>
</html>
  • A good question will include a question mark somewhere. What's yours? – Wyck Jan 10 '20 at 16:59
  • My apologies, how can I optimize my function so that the Population (P) can be arranged in sets of (S) 10 times and each set be unique, with no matching pairings, in each arrangement? – WesternWarrior Jan 10 '20 at 17:02
  • As a further improvement, please edit your question and put your code into the snippet editor (press Ctrl+M while editing your question) to make it runnable. Then others can see your code run right here, in its current incarnation. – Wyck Jan 10 '20 at 17:09
  • This is confusing, from my point of view it is in a snippet editor, and I just tried it out to make sure, is the code not in gray boxes from your view? – WesternWarrior Jan 10 '20 at 17:15
  • You're thinking _Code Sample_. Understandable if you've never used runnable snippets before. [Here's how](https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/). It's the 7th icon from the left when you're editing. Bold | Italic | Hyperlink | BlockQuote | Image | **JavaScript/HTML/CSS snippet**. – Wyck Jan 10 '20 at 17:57
  • I see it is a snippet now. Thanks. – Wyck Jan 10 '20 at 17:58

0 Answers0