Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!217.188.199.168.MISMATCH!takemy.news.telefonica.de!telefonica.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1744237.gKV8rdnPMv@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Tue, 17 Dec 2013 15:12:55 +0100 User-Agent: KNode/4.11.3 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <1l7578dngtzld$.19qzw1e0gy93t$.dlg@40tude.net> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 81 NNTP-Posting-Date: 17 Dec 2013 15:12:59 CET NNTP-Posting-Host: 10bdd5a9.newsspool4.arcor-online.net X-Trace: DXC=_2WFMf5 On Tue, 17 Dec 2013 07:06:55 +0700, JJ wrote: >>> Suggestion or Code very welcome. >>> >>> btw, I can supply *my* code down to producing a correect date value, >>> but it is clumsy and embarassing. But I will if you insist. >> >> Fix the date string then make them to a Date object so that you can get >> the timestamp integer value. >> >> function parseDate(dateStr) { >> //for "Mmm dd/yy" format, year 20nn only. >> //converts to "Mmm dd yyyy" (good for FF, IE8, Chrome, Opera). >> //returns NaN if dateStr is in invalid format. >> var date=dateStr.replace(/([a-z]+ \d+)\/(\d+)/,"$1 20$2"); return (new >> Date(date)).valueOf(); >> } > > To walk an array of numbers once to find the min and max values: > > var low, high, i; > low = high = arr[0]; > for( i = 1; i < arr.length; i++ ) { > if ( arr[i] < low ) low = arr[i]; > if ( arr[i] > high ) high = arr[i]; > } > > // low and high are now the minimum and maximum values from the array In implementations of ECMAScript Ed. 3, it is easier and probably more efficient to use the built-in Math.min() and Math.max() methods: var low = Math.min.apply(Math, arr); var high = Math.max.apply(Math, arr); They also work for Date instances as Array elements because they convert the arguments to Number before comparison. In general, if these methods are implemented, it is easiest to overwrite the “valueOf” property of the prototype object of the objects for the call – or provide the objects themselves with one –, which is triggered first by the internal ToNumber algorithm when called with hint Number. One can also access the first and last element of a sorted array: var sortedCopy = arr.slice().sort(function (a, b) { return a - b; }); var low = sortedCopy[0]; var high = sortedCopy[sortedCopy.length - 1]; Finally, all implementations of ECMAScript Ed. 5 – not just JavaScript anymore – provide Array.prototype.reduce(), as mentioned by Björn: var low = arr.reduce( function (accumulator, e, i, a) { if (е < accumulator) { return e; } return accumulator; }, arr[0]); There appears to be no point to use .map() here because if you have .map() you have .reduce() and can thereby shorten the scope chain. -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.