|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
468 bytes
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | function median(nums) {
|
|---|
| 6 | if (nums.length === 0) {
|
|---|
| 7 | return NaN;
|
|---|
| 8 | }
|
|---|
| 9 | const sorted = nums.slice().sort((a, b) => a - b);
|
|---|
| 10 | const middleIndex = Math.floor(sorted.length / 2);
|
|---|
| 11 | if (sorted.length % 2 === 0) {
|
|---|
| 12 | return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
|
|---|
| 13 | }
|
|---|
| 14 | else {
|
|---|
| 15 | return sorted[middleIndex];
|
|---|
| 16 | }
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | exports.median = median;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.