-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathAll Star Code Challenge #14.js
26 lines (20 loc) · 1.01 KB
/
All Star Code Challenge #14.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
Description:
This Kata is intended as a small challenge for my students
All Star Code Challenge #14
Your non-profit company has assigned you the task of calculating some simple statistics on donations. You have a very LONG array of integers, representing various amounts of donations your company has been given. In particular, you're interested in the median value for donations.
The median is the middle number of a sorted list of numbers. If the list is of even length, the 2 middle values are averaged.
Write a function called median() that takes an array of integers as an argument and returns the median of those integers.
Note:
The sorting step is vital
median([33,99,100,30,29,50]) // => 41.5
median([3,2,1]) // => 2
*/
function median(array) {
array=array.sort((a,b)=>a-b)
if (array.length%2===0){
let arr = array.slice( Math.floor(array.length/2)-1, Math.ceil(array.length/2)+1)
return arr.reduce((a,b)=>a+b,0)/arr.length
}
return array.slice( Math.floor(array.length/2), Math.ceil(array.length/2))*1
}