SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    mootools if possible some number how to explode each digit

    if I had a number like 123 how could I, with mootools if possible, get the index of each number so I can have something like

    var thenumber = 123;
    var firstvar = thenumber.indexOf(0);
    etc...

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,707
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    You can use basic JavaScript techniques to turn the number in to a string, break it apart in to an array of separate characters, and then use indexOf to find the position of a character from that array.

    Code javascript:
    var thenumber = 123;
    var arr = thenumber.toString().split(''); // ['1', '2', '3']
    var firstvar = arr.indexOf(0) // -1 which means, not found in the array

    Or, are you wanting something else? Are you wanting to retrieve each part of the number?

    With an array, you can use push/pop, which works on the end of an array. You can also use unshift/shift to work with the start of the array

    Code javascript:
    var thenumber = 123;
    var arr = thenumber.toString().split(''); // ['1', '2', '3']
    var firstvar = arr.shift(); // firstvar is '1' and arr is now ['2', '3']
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    You can use basic JavaScript techniques to turn the number in to a string, break it apart in to an array of separate characters, and then use indexOf to find the position of a character from that array.

    Code javascript:
    var thenumber = 123;
    var arr = thenumber.toString().split(''); // ['1', '2', '3']
    var firstvar = arr.indexOf(0) // -1 which means, not found in the array

    Or, are you wanting something else? Are you wanting to retrieve each part of the number?

    With an array, you can use push/pop, which works on the end of an array. You can also use unshift/shift to work with the start of the array

    Code javascript:
    var thenumber = 123;
    var arr = thenumber.toString().split(''); // ['1', '2', '3']
    var firstvar = arr.shift(); // firstvar is '1' and arr is now ['2', '3']
    thank you, the code bellow is what I was looking for

    Code JavaScript:
    some_string.charAt(0);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •