Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-31-2012, 04:39 AM   PM User | #16
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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>
jmrker is offline   Reply With Quote
Old 12-31-2012, 04:48 AM   PM User | #17
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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.
Logic Ali is offline   Reply With Quote
Old 12-31-2012, 02:57 PM   PM User | #18
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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>
jmrker is offline   Reply With Quote
Old 12-31-2012, 04:05 PM   PM User | #19
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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.
Logic Ali is offline   Reply With Quote
Old 12-31-2012, 04:45 PM   PM User | #20
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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!
jmrker is offline   Reply With Quote
Old 12-31-2012, 05:16 PM   PM User | #21
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
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)
007julien is offline   Reply With Quote
Old 12-31-2012, 05:36 PM   PM User | #22
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 01-01-2013, 09:05 PM   PM User | #23
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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..
triko is offline   Reply With Quote
Old 01-01-2013, 09:13 PM   PM User | #24
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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!!!!
triko is offline   Reply With Quote
Old 01-01-2013, 11:08 PM   PM User | #25
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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>
jmrker is offline   Reply With Quote
Old 01-03-2013, 05:21 PM   PM User | #26
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
OOOOOOOOOOOOooh!
It's so cool!!!! Thanks so much guy!!!
triko is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:22 PM.


Advertisement
Log in to turn off these ads.