Snake through Javascript Array

this is probably the simple answer, but I really laugh, and it really breaks my brain. I am trying to assign values ​​to variables while wading through an array.

My code is for the TTRPG tool for Discord, written in Discord.js. For this particular function, I want it to roll up videos depending on the number of players entered, and then combine all these videos and sort them. From there, I want him to snake through a sorted array to give each player a set of statuses so that each player is as close as possible to an equal playing field.

For example, if the input is 3 players, the bot will roll 3 sets of 6 stats and combine them into an array. For simplicity of explanation, we say that we flipped all the numbers from 1-18.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Will be assigned

ABCCBAABCCBAABCCBA

So that the final variables are

A = [1, 6, 7, 12, 13, 18]

B = [2, 5, 8, 11, 14, 17]

C = [3, 4, 9, 10, 15, 16]

The code that I have right now only sorts them by looping through (A, B, C, A, B, C ...), which does not average the players. I tried a bunch of different ways to get the results I need, but either the final variables are only assigned once, leaving the average variables with more statistics assigned to them, or each player variable is assigned only one stat.

, Googling "Javascript" "Snake" , , , , , . , , , , , , , !

:

if (msgContent.startsWith(".dstats ")) {
  let args = msgContent.split(" ").slice(1);
  var regex = /^\d+$/;
  var statIndex = [];
  var reply;
  var forward = true;
  if(regex.test(args) && args <= 10){
    for(var i = 0; i < args*6; i++){
      statRoll();
      statIndex.push(randStat);
    };
    distSort = statIndex.sort(sortNumber);
    for( j = 0; j < args; j++){
      this['player'+j] = [];
    };
    var playIndex = 0;
    for( f = 0; f < distSort.length; f++){
      if(playIndex < args && playIndex >= 0){
        this['player'+playIndex].push(distSort[f]);
      }else {
        playIndex = 0;
        this['player'+playIndex].push(distSort[f]);
      };
      playIndex++;

    };
    reply = "Your stats blocks are as follows:\n";
    for (k = 0; k < args; k++){
      reply += "Player " + (k+1) +": [" + this['player'+k].join(', ') + "]\n";
    };
    msg.reply(reply);
  }else(
    msg.reply("Looks like you inputted an improper number or your number is too high. Check your command and try again!")
  );
}
+4
3

JSON, , .

: https://jsbin.com/quvexu/edit?js,console

// Declare input and output variables
arr1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18];
arr2 = ['a','b','c','c','b','a','a','b','c','c','b','a','a','b','c','c','b','a'];
results = {a:[], b:[], c:[]};

// Do the work
arr2.forEach(function(d,i){ results[d].push(arr1[i]); })

// Print the results 
console.log("Results", results);
Hide result
+1

, .

var length = 6,
    lines = 3,
    result = [],
    i, j;
    
for (i = 0; i < length; i++) {
    for (j = 0; j < lines; j++) {
        result[j] = result[j] || [];
        result[j].push(i * lines + (i % 2 ? lines - j : j + 1));
    }
}

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Hide result
+1

You can do the following. He will handle the players nand will have 6 points for each player in the order in which you describe.

function dealPoints(c){
  var nums = Array(6*c).fill().map((_,i) => i+1);
  return nums.reduce(function(r,n,i){
                       var j = i%c;
                       j === 0 && (r.switch = !r.switch);
                       r.switch ? r[j].push(n) : r[c-j-1].push(n);
                       return r;
                     }, Array(c).fill().map(_ => []));
}
var players = 3,
     result = dealPoints(players);

console.log(result);
Run codeHide result
+1
source

Source: https://habr.com/ru/post/1670942/


All Articles