2

I have a set of data for 5 people. The data goes: "#ofKeys","PreferedCrate","Round1Results","Round2Results","Round2Results"

What I would like is for the data to be in some form of a 2D array so that it covers all information about all 5 players.

I will then like for the data to log the average of the 3 rounds for each player, as well as best and worst result that each player got.

I am not really sure how to approach this, so any help would be appreciated.

So far all I have really done is made a little array of items as follows:

var playerdata = [[2, Assault, 1,1,10],
                  [0, Support, 3,2,11],
                  [1, Assault, 7,12,1],
                  [0, Assault, 5,9,14],
                  [0, Assault, 11,17,18]];
evilgenious448
  • 518
  • 2
  • 11

3 Answers3

2

While you asked about more round results, you could treat the array from index 2 as data for the rounds.

var playerdata = [[2, 'Assault', 1, 1, 10], [0, 'Support', 3, 2, 11], [1, 'Assault', 7, 12, 1], [0, 'Assault', 5, 9, 14], [0, 'Assault', 11, 17, 18]],
    result = playerdata.map(player => {
        const data = player.slice(2);
        return {
            player,
            average: data.reduce((a, b) => a + b) / data.length,
            best: Math.max(...data),
            worst: Math.min(...data)
        };
  });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You can map your playerdata array and calculate the average/best/worst for each player:

var playerdata = [[2, 'Assault', 1,1,10],
                  [0, 'Support', 3,2,11],
                  [1, 'Assault', 7,12,1],
                  [0, 'Assault', 5,9,14],
                  [0, 'Assault', 11,17,18]];
                  
var calculated = playerdata.map((player) => {
  const rounds = player.slice(2);
  
  return {
    player,
    average: average(rounds),
    best: Math.max(...rounds),
    worst: Math.min(...rounds)
  };
});

function average(numbers) {
  return numbers.reduce((a, b) => a + b, 0) / numbers.length;
}

console.log(calculated);

console.log('Player 1 average', calculated[0].average);
SimpleJ
  • 13,812
  • 13
  • 53
  • 93
  • I have 2 questions about this. The first is, if I were to add more parameters, such as more rounds or more players, what lines of code would I need to modify. My second question is, how would I log individual player averages as in, if I need only the player 1 average, how do I obtain that? – evilgenious448 May 26 '17 at 16:25
  • I've edited my answer to account for a variable number of results and printing an individual player's stats. – SimpleJ May 26 '17 at 16:31
1

What makes the most sense (to me) is to map the data into objects for each player, like so:

This solution makes use of:

Array.prototype.map() (For mapping into array of objects)

Array.prototype.reduce()(For summing the rounds data generically)

ES6 Spread Syntax (For gathering/spreading rounds data)

ES6 Destructuring Assignment (For easily extracting player data into local variables)

ES6 Find (For plucking a single object out of an array by property value)

var playerdata = [[2, "Assault", 1,1,10],
                  [0, "Support", 3,2,11],
                  [1, "Assault", 7,12,1],
                  [0, "Assault", 5,9,14],
                  [0, "Assault", 11,17,18]];
                  
var mapped = playerdata.map(function (arr, index) {
    // Extract player data from array through Destructuring
    // Same as var NumberOfKeys = arr[0], PreferredCrate = arr[1], etc.
    // gather the rest of the arr elements as rounds data
    var [NumberOfKeys, PreferedCrate, ...rounds] = arr;
    
    // return an object with the data we want back
    return {
        PlayerName: "Player " + (index + 1),
        NumberOfKeys,
        PreferedCrate,
        NumberOfRounds: rounds.length,
        BestResult: Math.max(...rounds),
        WorstResult: Math.min(...rounds),
        Average: rounds.reduce((total, round) => total + round) / 3
    };
});

function getPlayerAverage(playerName, data) {
  // find a player by PlayerName (can be done with any property on the player object)
  var playerData = data.find(player => playerName === player.PlayerName);
  // if player is found, return the average, else return "N/A"
  return playerData ? playerData.Average : "N/A";
}
console.log("Player 1 average: ", getPlayerAverage("Player 1", mapped));
console.log("Player 99 average: ", getPlayerAverage("Player 99", mapped));
console.log("--- mapped data ---");
console.log(mapped);
mhodges
  • 10,938
  • 2
  • 28
  • 46
  • I have 2 questions about this. The first is, if I were to add more parameters, such as more rounds or more players, what lines of code would I need to modify. My second question is, how would I log individual player averages as in, if I need only the player 1 average, how do I obtain that? – evilgenious448 May 26 '17 at 16:25
  • @evilgenious448 For more players, nothing would need to be changed. For more rounds, I will modify the code slightly to accommodate for that (it's actually less code), and I will add the code to get a single player's average. – mhodges May 26 '17 at 16:48
  • @evilgenious448 Updated. Take a look to see if that's what you need! – mhodges May 26 '17 at 16:57