Skip to content

Commit

Permalink
Merge pull request loverajoel#238 from soyuka/gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
loverajoel committed Feb 12, 2016
2 parents 1d7786c + f20e3c2 commit 9a9ee40
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions _posts/en/2016-02-10-array-average-and-median.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,22 @@ values /= count;

Now, to get the median steps are:
- sort the array
- get the middle value
- get the arethmic mean of the middle values

```javascript
let values = [2, 56, 3, 41, 0, 4, 100, 23];
values.sort((a, b) => a - b);
let middle = Math.floor(values.length / 2);
let median = values[middle];
// median = 23
let lowMiddle = Math.floor((values.length - 1) / 2);
let highMiddle = Math.ceil((values.length - 1) / 2);
let median = (values[lowMiddle] + values[highMiddle]) / 2;
// median = 13,5
```

Or with a [right shift](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Right_shift) operator:
With a bitwise operator:

```javascript
let values = [2, 56, 3, 41, 0, 4, 100, 23];
values.sort((a, b) => a - b);
let median = values[values.length >> 1];
// median = 23
let median = (values[(values.length - 1) >> 1] + values[values.length >> 1]) / 2
// median = 13,5
```

0 comments on commit 9a9ee40

Please sign in to comment.