Skip to content

Commit

Permalink
es 6
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanvamsi3 committed Oct 6, 2017
1 parent df6dd0b commit 0a8b91c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
36 changes: 18 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
* Mean, Median, Mode
*/

var mean = numbers => {
let mean = numbers => {
if (!Array.isArray(numbers)) {
return null;
}
return numbers.reduce((pv, cv) => pv + cv, 0) / numbers.length;
}

var median = numbers => {
let median = numbers => {
if (!Array.isArray(numbers)) {
return null;
}
var median = 0, numsLen = numbers.length;
let median = 0, numsLen = numbers.length;
numbers.sort();

if (numbers.length % 2 == 0) {
Expand All @@ -26,19 +26,19 @@ var median = numbers => {
return median;
}

var mode = numbers => {
let mode = numbers => {
if (!Array.isArray(numbers)) {
return null;
}
var modes = [], count = [], i, number, maxIndex = 0;
for (i = 0; i < numbers.length; i += 1) {
number = numbers[i];
count[number] = (count[number] || 0) + 1;
if (count[number] > maxIndex) {
maxIndex = count[number];
}
let modes = [], count = [], maxIndex = 0;

for (let value of numbers) {
count[value] = (count[value] || 0) + 1;
if (count[value] > maxIndex)
maxIndex = count[value];
}
for (i in count) {

for (let i of numbers) {
if (count.hasOwnProperty(i)) {
if (count[i] === maxIndex) {
modes.push(Number(i));
Expand All @@ -49,25 +49,25 @@ var mode = numbers => {
return modes;
}

var standardDeviation = numbers => {
let standardDeviation = numbers => {
if (!Array.isArray(numbers)) {
return null;
}

return Math.sqrt(variance(numbers));
}

var variance = numbers => {
let variance = numbers => {
if (!Array.isArray(numbers)) {
return null;
}
var initialMean = mean(numbers);
var varianceNumbersArray = [], i;
let initialMean = mean(numbers);
let varianceNumbersArray = [], i;
for(i = 0; i < numbers.length; i += 1) {
var varianceNumber = numbers[i] - initialMean;
let varianceNumber = numbers[i] - initialMean;
varianceNumbersArray.push(varianceNumber * varianceNumber);
}
var variance = mean(varianceNumbersArray);
let variance = mean(varianceNumbersArray);

return variance;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "math-stats",
"version": "0.2.1",
"version": "0.2.2",
"description": "Mean, Median, Mode",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ var expect = require('chai').expect;
var stats = require('../index');

describe('#stats', function() {
it('should return 2', function() {
it('Median', function() {
var result = stats.median([1,2,3]);
expect(result).to.equal(2);
});
it('should throw not an array error', function() {
it('Not an array error', function() {
var result = stats.median(1);
expect(result).to.equal(null);
});
it('should return 1', function() {
it('Mode', function() {
var result = stats.mode([1, 2, 1, 3]);
expect(result[0]).to.equal(1);
});
it('should return 3', function() {
it('Mean', function() {
var result = stats.mean([1, 2, 3]);
expect(result).to.equal(2);
});
it('should return 0.816', function() {
it('Standard Deviation', function() {
var result = stats.standardDeviation([1, 2, 3]);
expect(result).to.equal(0.816496580927726);
})
it('should return 0.666', function() {
it('Variance', function() {
var result = stats.variance([1, 2, 3]);
expect(result).to.equal(0.6666666666666666);
})
Expand Down

0 comments on commit 0a8b91c

Please sign in to comment.