The array object has no method for finding the minimum value of an array of
numbers and the Math.min() method doesn't work on Arrays right out of the
box. I'll have similar issues if i wanted to return the maximum number, the
mean, mode,  median or standard deviations in any array of numbers.

This is a bit tasky and can be done in just a line of code if there where
Array.prototype.{method}() for it.

My proposal is, Arrays should have the following prototypes

   1. Array.prorotype.min()
   2.  Array.prorotype.max()
   3. Array.prorotype.mean()
   4. Array.prorotype.median()
   5. Array.prorotype.mode()
   6. Array.prototype.stdev()

The mean, median and mode would be very useful when visuali
Here's is a polyfill of what i'll love to propose.

//Array.prototype.min()
Array.prototype.min = function(){
let arrayVal = this;
let checkNonNumbers = this.some(element => {
if(typeof(element) !== typeof(Math.random())){
throw new Error("This Array.prorotype.min() takes only array of numbers as
arguments");
}
else{
return true;
}
});
function findMin(validated){
if(validated == true){
return Math.min.apply( Math, arrayVal );
}
}
return findMin(checkNonNumbers);
}

Similarly, Array.prototype.max() can be implemented as above.



//Array.prototype.median()
Array.prototype.median = function(){
let values = this;
function median(values){
values.sort(function(a,b){
return a-b;
});
if(values.length ===0) return 0
var half = Math.floor(values.length / 2);
if (values.length % 2)
return values[half];
else
return (values[half - 1] + values[half]) / 2.0;
}
return median(numbers)
}
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to