Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Reach a digit of numerical value in array index. Date: Thu, 19 Mar 2015 16:13:08 +0100 Organization: PointedEars Software (PES) Lines: 29 Message-ID: <4183666.fxmPkqLfvv@PointedEars.de> References: <7bb55099-4d9b-46e0-b4b7-3785304be4f4@googlegroups.com> <49b891b6-283a-45ed-beec-3cdc19983595@googlegroups.com> <9k5lgatqlm6esvojkhsntbcdpk58ma2gka@4ax.com> <2641901.31sAzN5gEV@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1426778116 3821 eJwFwYEBACAEBMCVKP8yDsr+I3SHTWW7ETQMxliNU6BjUkL6FNcOlRmNeu9p1pJ7OxLw6g8b8BGO (19 Mar 2015 15:15:16 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Thu, 19 Mar 2015 15:15:16 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNwQkBgAAIA8BKU946wKB/BL0z8ccn1M3Vzg7RVDG54mmCUpJFzE0AbFXdvsLPN3d2rBrMNzJsKPEBbYUWOA== Cancel-Lock: sha1:VLfu9eA81pf+4/WaQh3xyDCH9iI= X-NNTP-Posting-Host: eJwFwYEBwCAIA7CXFGjrzgEH/59gAufmVRAMDOaOuA1MD6AgVZudzvzi1xr1obDQ18xRyQcQ3RCq Xref: csiph.com comp.lang.javascript:27077 Thomas 'PointedEars' Lahn wrote: > […] there are (at least) three ways to retrieve the decimal > digit from a Number value without a "temporary variable"; just error-prone > and not very efficient ones (so kids, don’t do this at home): > > a) n.toString().charAt(i) > b) n.toString().slice(-j, -j + 1) > c) (n % Math.pow(10, j)).toString().charAt(0) > > where “n” is (a variable storing) the Number value (preferably a numeric > integer), “i” is (a variable storing) the 0-based index of the digit > counted from the left, and “j” is (a variable storing) the 1-based index > of the digit counted from the right. > > I do not think I need to explain why these approaches are not very > efficient. They are also error-prone because of limited precision of the > IEEE 754 double-precision floating-point format as such, and of the string > representation of its values in ECMAScript. For recreation, here is another one: d) Math.floor((n % Math.pow(10, j)) / Math.pow(10, j - 1)) -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.