Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!aioe.org!.POSTED!not-for-mail From: Joao Rodrigues Newsgroups: comp.lang.javascript Subject: Re: Reach a digit of numerical value in array index. Date: Sun, 22 Mar 2015 21:26:29 -0300 Organization: Aioe.org NNTP Server Lines: 54 Message-ID: References: <7bb55099-4d9b-46e0-b4b7-3785304be4f4@googlegroups.com> <49b891b6-283a-45ed-beec-3cdc19983595@googlegroups.com> <9k5lgatqlm6esvojkhsntbcdpk58ma2gka@4ax.com> <2641901.31sAzN5gEV@PointedEars.de> <8127222.NEJAP2j12Q@PointedEars.de> NNTP-Posting-Host: OsjKjz7AuNrX651V1sABXg.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.javascript:27105 Thomas 'PointedEars' Lahn wrote: > Dr J R Stockton wrote: > >> Consider and test >> >> >> j = 3 >> n = 12345 ; X = (n % Math.pow(10, j)).toString().charAt(0) >> n = 12045 ; Y = (n % Math.pow(10, j)).toString().charAt(0) >> n = 00345 ; Z = (n % Math.pow(10, j)).toString().charAt(0) >> Z = [X, Y, Z] ///// -> 3, 4, 2 > > Right, you would need to pad the toString() result if the result of the “%” > operation would be smaller than the 1-based index (j). I *did* test this > approach before posting it – even though I also *did* point out *twice* that > it is error-prone –, but not with such numbers. > > > Numeric integer literals with leading zero are parsed as octal in Mozilla > JavaScript (and not a syntax error) when not in strict mode. No surprise > here. > >> **TESTED** in current Firefox. > > Confirmed in Iceweasel 31.5.0 > Confirmed in Chrome 41 and IE 11 as well. 0345 // 229 [Z] 03458 // 3458 077 // 63 078 // 78 01234567 // 342391 012345678 // 12345678 0123456789 // 123456789 [1] A conforming implementation, when processing strict mode code (#10.1.1), must not extend the syntax of NumericLiteral to include OctalIntegerLiteral as described in B.1.1. In ECMAScript 6, octal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "O" (0o or 0O). -- Joao Rodrigues