How can I create arrays within a loop (within another loop) and pass them back out in JavaScript?

I'm using Google Docs Spreadsheet API to keep track of a competition between some of my friends. I have some big ideas, but I'm stumped right now. I'm trying to create 20 different arrays inside of loops (see below) and then evaluate each one outside of the loop, and I really don't want to write 20 "if...then" statements.

NOTE: the following SUMMARY may or may not help you answer my question. You might want to skip down to the code, then read this if you need it :)

Summary of the program: Each player assigns point values in favor of one possible outcome of a set of binary-outcome events. As the events happen, players either gain the points assigned if their outcome occurs, or gain no points if the opposite outcome occurs. My goal is to 1) figure out exactly when a player is eliminated, and 2) highlight all remaining events that must be won for them to have a chance at tying for first.

Instead of trying to somehow evaluate all possibilities (5 players picking, 2^16 outcomes... I have zero computer science knowledge, but this seems like an incredibly huge task even for the modern computer) I've come up with an alternate idea. The script loops through each player, against each other opponent. It calculates the maximum number of points a player can score based on their value assignments and the already determined game. For one player and one opponent, it checks the best possible outcome by the player against that opponent, and if there is any opponent he cannot beat, even in the best case, then he is eliminated.

This part is easy-- after the loop runs inside the loop, I just adjust a global variable that I created earlier, and when the outer loop is done, just grab those variables and write them to the sheet.

Unfortunately, this misses the case of where he could have a best case against each individual opponent, but not against multiple opponents at once.

So the next step is what I'm trying to do now. I'm not even sure I can give a good explanation without just showing you the entire spreadsheet w/script, but I'll try. So what I want to do now is calculate the "value" of each event for each player against a given other player. If both player and opponent assigned points in favor of the same event outcome for one event, the event's value is the difference between the picks (positive if player picked higher, negative if lower), and it's the SUM if they picked opposite event outcomes. Now, I do the same thing as before-- take a best-case scenario for a player against a given opponent-- but now I check by how much the player can beat the opponent in a best-case scenario. Then I evaluate the (absolute value of the) event value against this difference, and if it's greater, then the event is a must win (or must lose if the event value is negative). And, if an event is both a "must-win" and a "must lose" event, then the player is eliminated.

The problem is that this second step requires me to create a new array of values for each player-opponent combination, and then do things with the values after they're created.

I realize one approach would be to create 20 different arrays, and throughout the entire loops, keep checking if (player == "1" && opponent == 2){} and populate the arrays accordingly, but this seems kind of ridiculous. And more importantly, this entire project is my attempt at learning JavaScript, so what's the point in using a time-intensive workaround that doesn't teach me anything new?

I'm trying to understand square bracket notation, since it seems to be the answer to my question, but a lot of people are also suggesting that it's impossible to create variable names by concatenating with the value of another variable... so anyway, here's what I'm trying. I'd really appreciate either a fix to my approach, or a better approach.

for (var player=1; player<6; player++){
  if(player==1){look up certain columns in the spreadsheet and save them to variables}
  //ditto for other players

  for(var opponent=1; opponent<6; opponent++){
      if(opponent==1){save more values to variables}
      //ditto for other players

  for(var row=9; row<24; row++) {
//Now the script goes down each row of an array containing the original
//spreadsheet info, and, based on information determined by the variables
//created above, get values corresponding to the player and opponent.
//So what I'd like to do here is create "array[1,2]==" and then "array[1,3]=="
//and so forth, creating them globally (I think I understand that term by now) 
//so I can refer to them outside of the loops later to do some evaluatin'.
//get array[1,2]...array[5,4] and do some operations with them.

I really looking forward to your advice and ideas!


  • How can I create arrays within a loop (within another loop)?

    Code update 2

    As you said: "i am trying to understand square bracket notation" You may take a look at my new demo and the code:

    function getTeam(){
        var array = [[1,2,3],[4,5,6],[7,8,9]]; // arrays within arrays
        // array myTeam 
        var myTeam = [[],[],[],[]];
        var playerNames = ["John", "Bert", "Dave", "Milton"]; 
        var ages =[]; 
        var weight = 104;
        // loop over the team arrayadd each player (name, age and weight) to the team  
        for (i=0; i < myTeam.length; i++){            
            // fill the age array in a loop
            for (j=0;j<myTeam.length;j++) {
                ages[j] = 23 + j;            
            myTeam[i].push([playerNames[i], ages[i], weight]);
        return myTeam;

    And pass them back out in Javascript

    Could you elaborate on this part?


    var valuesOfPlayers=[];
    for (var player=1; player<6; player++){
      // look up certain columns in the spreadsheet and save them to variables
      // you could call a funcntion and return the values you 
      // collected in an array within an array as in the demo above
      valuesOfPlayers[player] = lookupColumnValues(player);
      for(var opponent=1; opponent<6; opponent++){
           // save more values to variables
           valuesOfPlayers[player] = addValuesToVar(player);
      for(var row=9; row<24; row++) {
           // if you collect the values in your first and second if clause
           // what other information do you want to collect
           // Please elaborate this part?