Skip to content

Commit

Permalink
Merge pull request #28 from viktorzavadil/master
Browse files Browse the repository at this point in the history
Fix median function: sort by numbers
  • Loading branch information
pavanvamsi3 authored Jun 8, 2022
2 parents 20bffc7 + e0c6300 commit 6181b06
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ let median = numbers => {
return null;
}
let median = 0, numsLen = numbers.length;
numbers.sort();
numbers = numbers.sort((a, b) => a - b);

if (numbers.length % 2 == 0) {
if (numbers.length % 2 === 0) {
median = (numbers[numsLen / 2 - 1] + numbers[numsLen / 2]) / 2;
} else {
median = numbers[(numsLen - 1) / 2];
Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ describe('#stats', function() {
var result = stats.median(1);
expect(result).to.equal(null);
});
it('Median from numbers with more ciphers', function() {
var result = stats.median([1, 2, 22, 3, 33]);
expect(result).to.equal(3);
});
it('Not an array error mean', function() {
var result = stats.mean(1);
expect(result).to.equal(null);
Expand Down

0 comments on commit 6181b06

Please sign in to comment.