Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27093

Re: Reach a digit of numerical value in array index.

From Dr J R Stockton <reply1500@merlyn.demon.co.uk.invalid>
Newsgroups comp.lang.javascript
Subject Re: Reach a digit of numerical value in array index.
Date 2015-03-20 23:50 +0000
Organization Home
Message-ID <fbSfNlNPJLDVFwke@invalid.uk.co.demon.merlyn.invalid> (permalink)
References <7bb55099-4d9b-46e0-b4b7-3785304be4f4@googlegroups.com> <medgv0$gjb$1@dont-email.me> <49b891b6-283a-45ed-beec-3cdc19983595@googlegroups.com> <9k5lgatqlm6esvojkhsntbcdpk58ma2gka@4ax.com> <2641901.31sAzN5gEV@PointedEars.de>

Show all headers | View raw


In comp.lang.javascript message <2641901.31sAzN5gEV@PointedEars.de>,
Thu, 19 Mar 2015 15:59:25, Thomas 'PointedEars' Lahn
<PointedEars@web.de> posted:

>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.

Efficiency may not be important, but correctness usually is.
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

in which Z is present for amazement/amusement only.

**TESTED** in current Firefox.

-- 
 (c) John Stockton, nr London, UK.  For Mail, see Home Page.  Turnpike, WinXP.
 Web  <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
 Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
 DOS/Win/UNIX now 2.0.6;  see my <http://www.merlyn.demon.co.uk/pc-links.htm>.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-17 14:15 -0700
  Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-17 14:34 -0700
    Re: Reach a digit of numerical value in array index. JJ <jj4public@vfemail.net> - 2015-03-18 13:38 +0700
      Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-18 06:57 -0700
  Re: Reach a digit of numerical value in array index. Norman Peelman <npeelman@cfl.rr.com> - 2015-03-18 23:49 -0400
    Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-19 01:16 -0700
      Re: Reach a digit of numerical value in array index. John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-19 09:28 +0000
        Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-19 05:00 -0700
          Re: Reach a digit of numerical value in array index. John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-19 16:23 +0000
            Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-19 10:43 -0700
              Re: Reach a digit of numerical value in array index. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-03-19 19:51 +0100
              Re: Reach a digit of numerical value in array index. John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-22 11:20 +0000
                Re: Reach a digit of numerical value in array index. jonas.thornvall@gmail.com - 2015-03-22 08:18 -0700
        Re: Reach a digit of numerical value in array index. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-19 15:59 +0100
          Re: Reach a digit of numerical value in array index. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-19 16:13 +0100
          Re: Reach a digit of numerical value in array index. Dr J R Stockton <reply1500@merlyn.demon.co.uk.invalid> - 2015-03-20 23:50 +0000
            Re: Reach a digit of numerical value in array index. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-22 16:29 +0100
              Re: Reach a digit of numerical value in array index. Scott Sauyet <scott@sauyet.com> - 2015-03-22 17:36 -0400
              Re: Reach a digit of numerical value in array index. Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-03-22 21:26 -0300
                Re: Reach a digit of numerical value in array index. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-23 05:54 +0100

csiph-web