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