Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1693047.RCSKjYtSyA@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Tue, 17 Dec 2013 21:42:38 +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> <1744237.gKV8rdnPMv@PointedEars.de> MIME-Version: 1.0 Lines: 47 NNTP-Posting-Date: 17 Dec 2013 21:42:41 CET NNTP-Posting-Host: 5213bbd4.newsspool1.arcor-online.net X-Trace: DXC=a9KZY?EF=OB74okIm;?DS@ic==]BZ:afN4Fo<]lROoRAnkgeX?EC@@@IRTKiQcV`1DDZm8W4\YJNLYUVHKH;GI^GS:IUVY_FQ[K6lN=>XZ6RC@ X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:22360 Joao Rodrigues wrote: > On 17/12/2013 12:12, Thomas 'PointedEars' Lahn wrote: >> 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]; >> >> > > or > > var low = sortedCopy.shift(); > var high = sortedCopy.pop(); If you do not need the original copy later. Because the property accessor does not modify the array like these two methods do. >> 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]); >> >> > > 'accumulator' is a bad name, as you are not accumulating anything in > that function. The name fits. An accumulator in computer science is a container that stores an intermediate value (remember the processor registers)? As you can see, it is directly from the Specification. -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.