class DataSet { constructor(...data) { this.data = data; } get mean() { return this._mean = (this._mean !== undefined) ? this._mean : this.data.reduce((a, b) => a + b) / this.data.length } get variance() { return this._variance = this._variance !== undefined ? this._variance : this.stdDeviation ** 2; } get stdDeviation() { return this._stdDeviation = (this._stdDeviation !== undefined) ? this._stdDeviation : Math.sqrt( this.data.map(x => x * x).reduce((a, b) => a + b) / this.data.length - (this.data.reduce((a, b) => a + b) / this.data.length) ** 2); } reset() { delete this._mean; delete this._variance; delete this._stdDeviation; } }
- class DataSet {
- constructor(...data) {
- this.data = data;
this.mean = this.data.reduce((a,b)=>a+b) / this.data.length;this.variance = this.data.map(x=>x*x).reduce((a,b)=>a+b) / this.data.length - this.mean ** 2;this.stdDeviation = Math.sqrt(this.variance);- }
setMean() {return this.mean = this.data.reduce((a,b)=>a+b) / this.data.length;- get mean() {
- return this._mean = (this._mean !== undefined) ? this._mean
- : this.data.reduce((a, b) => a + b) / this.data.length
- }
setVar() {this.stdDeviation = Math.sqrt(this.data.map(x=>x*x).reduce((a,b)=>a+b) / this.data.length - (this.data.reduce((a,b)=>a+b) / this.data.length) ** 2);return this.variance = this.stdDeviation ** 2;- get variance() {
- return this._variance = this._variance !== undefined ? this._variance
- : this.stdDeviation ** 2;
- }
- get stdDeviation() {
- return this._stdDeviation = (this._stdDeviation !== undefined) ? this._stdDeviation
- : Math.sqrt(
- this.data.map(x => x * x).reduce((a, b) => a + b) / this.data.length
- - (this.data.reduce((a, b) => a + b) / this.data.length) ** 2);
- }
- reset() {
- delete this._mean;
- delete this._variance;
- delete this._stdDeviation;
- }
- }
Test.describe("Your <code>DataSet()</code> Class", _ => { Test.it("should work for the example provided in the description", _ => { var myData1 = new DataSet(1,2,3,4,5,6,7); Test.assertSimilar(myData1.data, [1,2,3,4,5,6,7]); Test.assertEquals(myData1.mean, 4); Test.assertEquals(myData1.variance, 4); Test.assertEquals(myData1.stdDeviation, 2); myData1.data[6] = 14; myData1.reset(); Test.assertEquals(myData1.mean, 5); Test.assertEquals(myData1.variance, 16); Test.assertEquals(myData1.stdDeviation, 4); }); Test.it("should also work for my custom test cases", _ => { // Add your own test cases here :) }); });
- Test.describe("Your <code>DataSet()</code> Class", _ => {
- Test.it("should work for the example provided in the description", _ => {
- var myData1 = new DataSet(1,2,3,4,5,6,7);
- Test.assertSimilar(myData1.data, [1,2,3,4,5,6,7]);
- Test.assertEquals(myData1.mean, 4);
- Test.assertEquals(myData1.variance, 4);
- Test.assertEquals(myData1.stdDeviation, 2);
- myData1.data[6] = 14;
Test.assertEquals(myData1.setMean(), 5);- myData1.reset();
- Test.assertEquals(myData1.mean, 5);
Test.assertEquals(myData1.setVar(), 16);- Test.assertEquals(myData1.variance, 16);
- Test.assertEquals(myData1.stdDeviation, 4);
- });
- Test.it("should also work for my custom test cases", _ => {
- // Add your own test cases here :)
- });
- });