Dev Helpers
← Back

Median summary

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

Open this example in the interactive library

Live preview

Source code

// components/MedianSummary.tsx

export default function MedianSummary({
  values = [2, 3, 3, 6, 11],
}: { values?: number[] } = {}) {
  const v = [...values].sort((a, b) => a - b);
  const mid = Math.floor(v.length / 2);
  return (
    <output>
      Median: {v.length ? (v.length % 2 ? v[mid] : (v[mid - 1] + v[mid]) / 2) : "—"}
    </output>
  );
}