Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26

Thread: Help Array!!!!!

  1. #16
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,108
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    And adding to 'Logic Ali's brilliant code ...
    Code:
    <script type="text/javascript">
    
    array1 = [ 1, 4, 2, 13, 5 ];
    
    odds = array1.slice( 0 ).filter( function( elem ){ return elem % 2; } )
    evens = array1.slice( 0 ).filter( function( elem ){ return elem%2==0; } )
    
    alert( odds + '\n\n' + evens );
    
    </script>

  2. #17
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by jmrker View Post
    And adding to 'Logic Ali's brilliant code ...
    Code:
    <script type="text/javascript">
    
    array1 = [ 1, 4, 2, 13, 5 ];
    
    odds = array1.slice( 0 ).filter( function( elem ){ return elem % 2; } )
    evens = array1.slice( 0 ).filter( function( elem ){ return elem%2==0; } )
    
    alert( odds + '\n\n' + evens );
    
    </script>
    Actually I should have remembered that .slice is unnecessary with .filter.

  3. #18
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,108
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Thumbs up

    Even better...
    Code:
    <script type="text/javascript">
    
    array1 = [ 1, 4, 2, 13, 5 ];
    
    odds = array1.filter( function( elem ){ return elem % 2; } )
    evens = array1.filter( function( elem ){ return elem%2==0; } )
    
    alert( odds + '\n\n' + evens );
    
    </script>

  4. #19
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by jmrker View Post
    Even better...
    Code:
    <script type="text/javascript">
    
    array1 = [ 1, 4, 2, 13, 5 ];
    
    odds = array1.filter( function( elem ){ return elem % 2; } )
    evens = array1.filter( function( elem ){ return elem%2==0; } )
    
    alert( odds + '\n\n' + evens );
    
    </script>
    Now all you need is for Windows XP (IE8) to become 100% extinct, and you'll be able to use it in practice.

  5. #20
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,108
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Arrow

    Quote Originally Posted by Logic Ali View Post
    Now all you need is for Windows XP (IE8) to become 100% extinct, and you'll be able to use it in practice.
    Well bummer!

  6. #21
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    About commas... The window's alert method apply the method toString() at is content. Then it's possible to see simple objects with a toString() method for objects

    Code:
    Object.prototype.toString=function(){var i,c='\n';for (i in this) if (this.hasOwnProperty(i)) c+=i+':'+this[i]+'\n';return c}
    
    var NewYork={lat:"N 40° 42' 31’’",lng:"74° 0’ 34’’"}
    alert(NewYork)

  7. #22
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    The following code allows filter for older browsers: Mozilla

    Code:
    if (!Array.prototype.filter)
    {
      Array.prototype.filter = function(fun /*, thisp */)
      {
        "use strict";
     
        if (this == null)
          throw new TypeError();
     
        var t = Object(this);
        var len = t.length >>> 0;
        if (typeof fun != "function")
          throw new TypeError();
     
        var res = [];
        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
        {
          if (i in t)
          {
            var val = t[i]; // in case fun mutates this
            if (fun.call(thisp, val, i, t))
              res.push(val);
          }
        }
     
        return res;
      };
    }
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  8. #23
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    O god!! How much comment
    Guys I don t have never see all your methid .filter, \n\, ecc... ????
    I'm beginner of Javascript, i think that .join it's sufficient for a right code
    This is final code
    Code:
    <!DOCTYPE HTML>
    <html>
    <head> <title> Selection </title>
    <script type = "text/javascript">
    function evenOdd ()
    {
        var arrayPrincipal = [4, 2,53, 4, 12, 7, 20, 13, 0, 5];
        var arrayEven = [];
        var arrayOdd = [];
        for (i = 0; i < arrayPrincipal.length; i++)
        {
            var evenOdd = arrayPrincipal [i];
            var remainder = evenOdd%2;
            if (remainder == 0)
                arrayEven [i] = arrayPrincipal [i];
            else
                arrayOdd [i] = arrayPrincipal [i];
        }			
        alert ("This is Array Even" + " " + arrayEven.join (" "));
        alert ("This is Array Odd" + " " + arrayOdd.join (" "));
    }
    </script>
    <body>
    <button type = "button" onclick = "evenOdd ()"> Build Symmetric </button>
    </body>
    </html>
    My problem is that in all 2 array i have the position null!!! And then I write .join have other comma!!!!
    Last edited by triko; 01-01-2013 at 09:14 PM.

  9. #24
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    Actually I should have remembered that .slice is unnecessary with .filter.
    This is right but my problem ask, Build 2 new array and insert into one the even numbers and into other the odd numbers!!!!

  10. #25
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,108
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    The reason you are getting null in the array is that you are NOT assigning anything
    to the even/odd arrays when the test array is odd/even.
    Those positions are not defined when the test fails,
    and then you are skipping over the element positions
    when you increment the 'i' variable.

    The should be you "final code"
    Code:
    <!DOCTYPE HTML>
    <html>
    <head> <title> Selection </title>
    <script type = "text/javascript">
    var arrayPrincipal = [4, 2,53, 4, 12, 7, 20, 13, 0, 5];
    function evenOdd () {
        var arrayEven = [];
        var arrayOdd = [];
        for (i = 0; i < arrayPrincipal.length; i++) {
          if (arrayPrincipal[i]%2==0) { arrayEven.push(arrayPrincipal[i]); }
                                 else { arrayOdd.push(arrayPrincipal[i]); }
        }
        alert ("This is Array Even" + " " + arrayEven.join (" ")+"\nThis is Array Odd" + " " + arrayOdd.join (" "));
    }
    </script>
    <body>
    <button type = "button" onclick = "evenOdd()"> Build Symmetric </button>
    </body>
    </html>

  11. #26
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OOOOOOOOOOOOooh!
    It's so cool!!!! Thanks so much guy!!!


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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