www.webdeveloper.com
Results 1 to 2 of 2

Thread: user input long int to array

  1. #1
    Join Date
    Dec 2010
    Posts
    1

    Red face user input long int to array

    i am prompting the user to enter an integer through scanner and somehow need to turn that integer into an array (i.e. 12345 = int [] = {1,2,3,4,5})
    is there a way to do this? while also ignoring negatives and leading 0s ?

    thanks!

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,414

    Lightbulb Something to try ...

    Try this:
    Code:
    <script type="text/javascript>
    function int2arr(val) {
      var txt = /^(-|0*)/;   val = val.replace(txt,'');
      var num = Math.abs(val * 1).toString();
      return num.split('');
    }
    var num = '12345';   alert(int2arr(num).join(',')); // 1,2,3,4,5
        num = '-4321';  alert(int2arr(num).join(','));  // 4,3,2,1
        num = '01234';  alert(int2arr(num).join(','));  // 1,2,3,4
        num = '-0321';  alert(int2arr(num).join(','));  // 3,2,1
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles