# Help Array!!!!!

Printable View

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• 12-31-2012, 04:39 AM
jmrker
And adding to 'Logic Ali's brilliant code ... :thumbsup:
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>```
• 12-31-2012, 04:48 AM
Logic Ali
Quote:

Originally Posted by jmrker
And adding to 'Logic Ali's brilliant code ... :thumbsup:
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.
• 12-31-2012, 02:57 PM
jmrker
Even better...:thumbsup:
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>```
• 12-31-2012, 04:05 PM
Logic Ali
Quote:

Originally Posted by jmrker
Even better...:thumbsup:
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.
• 12-31-2012, 04:45 PM
jmrker
Quote:

Originally Posted by Logic Ali
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!
;) :rolleyes: :mad:
• 12-31-2012, 05:16 PM
007julien
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)```
• 12-31-2012, 05:36 PM
AndrewGSW
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;   }; }```
• 01-01-2013, 09:05 PM
triko
O god!! How much comment :D
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 :D
This is final code :D
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!!!!
• 01-01-2013, 09:13 PM
triko
Quote:

Originally Posted by Logic Ali
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!!!!
• 01-01-2013, 11:08 PM
jmrker
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>```
• 01-03-2013, 05:21 PM
triko
OOOOOOOOOOOOooh! :D
It's so cool!!!! Thanks so much guy!!!
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12