Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!feeder.erje.net!eu.feeder.erje.net!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 15:59:25 +0100 Organization: PointedEars Software (PES) Lines: 36 Message-ID: <2641901.31sAzN5gEV@PointedEars.de> References: <7bb55099-4d9b-46e0-b4b7-3785304be4f4@googlegroups.com> <49b891b6-283a-45ed-beec-3cdc19983595@googlegroups.com> <9k5lgatqlm6esvojkhsntbcdpk58ma2gka@4ax.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1426777266 836 eJwVysEBgDAIA8CVRJpAxqkC+49gvffBaXxjEVwYzJXV01s7qk2VrkD95QQZdUt0TJr3cyI+IMYRAg== (19 Mar 2015 15:01:06 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Thu, 19 Mar 2015 15:01:06 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNyskVACEIBNGUGKFbDYdlyD8EeXWpw4fyY24jaOhJ4pbAT1zYXN8dLimwHNWEUun5Z4VoFwe6Z62VvfTgPFuqFhs= Cancel-Lock: sha1:hq3gYMFfKlbhQ5UGiLYWP7w3hsc= X-NNTP-Posting-Host: eJwNyMkBwDAIA7CVymFTxgFS9h8h1VMwCiecoGOxZzUL+2RLvpWqfSAdIqSOhbI6Gub14c+RCxbYEJU= Xref: csiph.com comp.lang.javascript:27076 John Harris wrote: > […] jonas.thornvall@gmail.com wrote: >> Well i know howto do it, but its insane, >> i questioning why one can not read/reach the digit place >> of a number stored in an array, without going via temp variables. >> There should be a way reading them straight out of the index. > Why should there be? > > Remember that what's stored are binary numbers. Do you really want to > fetch a binary digit? ACK. That said, 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. -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.