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)
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
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!!!!
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>