> Math.min() method doesn't work on Arrays right out of the box. No, but rest parameters work: Math.min(...arr) is fine, and works with Sets too.
On Wed, 21 Nov 2018 at 14:08 Isiah Meadows <isiahmead...@gmail.com> wrote: > Not sure these belong in the standard library itself. I could see max and > min being there (these are very broadly useful), but the others, not so > much. I'd also like both max and min accept an optional comparison callback > of `(a, b) => a < b`. But the rest seem too narrowly useful IMHO. > On Tue, Nov 20, 2018 at 21:38 Gbadebo Bello <gbahdeybohbe...@gmail.com> > wrote: > >> 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 >> es-discuss@mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> > _______________________________________________ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss >
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss