Thread: Help Array!!!!!
View Single Post
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