jeudi 27 août 2020

Creating Many to one relationships using array of arrays

I am creating a teach the teacher program that should assign multiple and unique teachers to teach another teacher.

I am using an array of arrays to capture and randomize the groups and the list of teachers. In this script I am using 3 levels of arrays. Would this be inefficient for a long list and many groups?

    //Shuffle and create groups

    function shuffle(a) {
       for (let i = a.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
       [a[i], a[j]] = [a[j], a[i]];
     }
      return a;
    }

    const Tgroups = [[3,[1, 2]], [4,[5,6]], [7,[8,9]],[10,[11,12]]];
   //const Tgroups = [[1, 2], [5,6], [8,9],[11,12]];

   Tgroups.forEach(shuffle);
   shuffle(Tgroups);

   console.log("shuffled:", Tgroups.join(" | "))


   const Tlist = [].concat(...Tgroups);

   console.log("Tlist:", Tlist);

I have been able to pair one teacher with another.

    // match the group with the list:
    const pairs = [];

    for(let i = 0; i < Math.floor(Tlist.length / 2); i++) {
    pairs.push([
    Tlist[i],
    Tlist[i + Math.floor(Tlist.length / 2)]
    ]);
   }

  if(Tlist.length % 2)
  pairs.push([Tlist.pop()]);
  console.log("pairs:", pairs.join(" | "));

  const result = [].concat(...pairs);

However, I do not believe I am creating the many to one relation using the following

    for(let i = 0; i < result.length; i++)
     {
       var innerArrayLength = result[i].length;
       for (let j = 0; j < innerArrayLength; j++) 
     {
       //console.log('[' + i + ',' + j + '] = ' + result[i][j]);
       console.log(result[i] + " -> " + result[(i + 2) % result.length]);
     }
    

Please, is there a way to achieve a better result?




Aucun commentaire:

Enregistrer un commentaire