find the standard deviation of all values in a map in typescript

One way to find the standard deviation of all values in a map in TypeScript is to first obtain an array of values from the map using the Array.from() method. Then, we can calculate the mean of these values and use it to calculate the variance and standard deviation of the values using the following formula:

index.ts
variance = 1/n * sum((x - mean)^2)
standard deviation = sqrt(variance)
71 chars
3 lines

Here's an implementation of this in TypeScript:

index.ts
function calculateStandardDeviation(map: Map<any, number>): number {
  const valuesArray = Array.from(map.values());
  const sum = valuesArray.reduce((acc, val) => acc + val, 0);
  const mean = sum / valuesArray.length;
  const variance = valuesArray.reduce((acc, val) => acc + Math.pow(val - mean, 2), 0) / valuesArray.length;
  const standardDeviation = Math.sqrt(variance);
  return standardDeviation;
}
407 chars
9 lines

This function takes a map as an argument and returns the standard deviation of all values in the map.

gistlibby LogSnag