Dev HelpersOpen library
← Back

Population deviation

Import and render <PopulationDeviation /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/PopulationDeviation.tsx

export default function PopulationDeviation({
  values = [2, 3, 3, 6, 11],
}: { values?: number[] } = {}) {
  const avg = values.reduce((a, b) => a + b, 0) / (values.length || 1);
  const variance =
    values.reduce((s, x) => s + (x - avg) ** 2, 0) / (values.length || 1);
  return (
    <output>
      Standard deviation: {values.length ? Math.sqrt(variance).toFixed(2) : "—"}
    </output>
  );
}