I'm currently working on a code snippet that extracts data from an array and calculates the average. The problem I'm facing is that even if the calculated average is a whole number like 3, it still displays as 3.00. Ideally, I would like the average to only show up to 2 decimal places when necessary. Here's the code snippet:
var calculated = playerdata.map((player) => {
const rounds = player.slice(2);
return {
player,
average: average(rounds).toFixed(2),
best: Math.min(...rounds),
worst: Math.max(...rounds)
};
});
function average(numbers) {
return numbers.reduce((a, b) => a + b, 0) / numbers.length;
}