TheWalrusEffect -4 years ago 134
Node.js Question

# Snake Through Javascript Array

this is probably a simple answer, but I am a hobbiest and this is really breaking my brain. I'm trying to assign values to variables by snaking through an array.

My code is for a TTRPG toolbot for Discord written in Discord.js. For this particular feature, I want to have it roll n stat rolls depending on the number of players inputted, and then pool all those rolls together and sort them. From there, I want to have it snake through the sorted array to give each player a stat set so that each player is as close to being on a level playing field.

For example, if the input is 3 players, the bot will roll 3 sets of 6 stats and pool them to an array. For simplicity in explaining, we'll say we rolled all the numbers from 1-18.

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

Would get assigned to

A B C C B A A B C C B A A B C C B A

So that the final variables would

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

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

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

The code I have right now only sorts them by looping through (A, B, C, A, B, C...) which doesn't lead to the players being averaged out. I've tried a bunch of different ways to get the results I need, but either the end variables only get assigned once, leaving the middle variables with more stats assigned to them, or each player variable only is assigned one stat.

I've tried searching online for any help, but Googling anything with "Javascript" and "Snake" just teaches you how to make the game, so I'm really hoping you guys will be able to help me. Thank you so much, and I'm sorry if what I'm trying to say isn't clear, so I'm more than happy to answer any questions you might have to help get this answered!

Code:

``````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!")
);
}
``````

Answer Source

I'd suggest using JSON where you can in this situation, as it'll make it much easier to work with your data.

Anyway, the following code should do the trick: 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);``````

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download
Latest added