www.webdeveloper.com
Results 1 to 9 of 9

Thread: [RESOLVED] Adding to array from for loop

  1. #1
    Join Date
    May 2011
    Posts
    8

    resolved [RESOLVED] Adding to array from for loop

    Hi, When I try to write a value to the array, loop stops.

    Code:
    var colNums = new Array[5];
    
    function multiplicationTable(){
    	// Random Num Cols
    	for (var i=0; i<5; i++){
    		var randomColNum=Math.floor(Math.random()*100)+1;
    		document.getElementById("col_"+i).innerHTML = randomColNum;	
    		colNums[i]=randomColNum; //Trying to write value to array, stops loop
    	}
    Thanks for any help.

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,689
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script> vic=0; </script>
    <form name=Show id=Show style="position:absolute;visibility:visible;top:700px;left:0px;" >
    <input size=100 name=Show0 >
    <input size=10 name=Show1 >
    <input size=10 name=Show2 >
    <input size=10 name=Show3 >
    <input size=10 name=Show4 >
    <input size=10 name=Show5 >
    <input size=10 name=Show6 >
    <input size=10 name=Show7 >
    <input size=10 name=Show8 >
    <input size=10 name=Show9 ><br>
    <textarea name=TA rows=1 cols=100 ></textarea>
    </form>
    <script   type="text/javascript">
    /*<![CDATA[*/
    var colNums = [];
    
    //function multiplicationTable(){
    	// Random Num Cols
    	for (var i=0; i<5; i++){
    		var randomColNum=Math.floor(Math.random()*100)+1;
    //		document.getElementById("col_"+i).innerHTML = randomColNum;
    		colNums[i]=randomColNum; //Trying to write value to array, stops loop
    	}
    document.Show.Show0.value=colNums;
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    May 2011
    Posts
    8
    Changing:
    Code:
    var colNums = new Array[5];
    To:
    Code:
    var colNums = new Array[5];
    Did the trick.

    Thank you.

  4. #4
    Join Date
    May 2011
    Posts
    8
    I meant to var colNums = [5];
    Can you tell me what I don't need 'new Array'?
    Thank you.

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by acbmse View Post
    I meant to var colNums = [5];
    Can you tell me what I don't need 'new Array'?
    Thank you.
    colNums = [5] produces the following array: [5]...
    you over-write the only element first thing in your loop, so you never noticed the 5. javascript expands arrays to their highest numbered index, so adding elements in a loop expands the array each cycle.

    you really just need to say colNums= [];

  6. #6
    Join Date
    May 2011
    Posts
    8
    Thanks so much. That makes sense.
    I'm just starting to learn JavaScript and this is a big help.
    Just one more question if you don't mind, why don't we use "new Array".

  7. #7
    Join Date
    May 2011
    Posts
    8
    Sorry.
    In my original code I used; var colNums = new Array[5];

    When we change it to; var colNums = []; my script works.

    So, I guess my question is, what is the difference in using 'new Array' in the var and not using it?

    Hope this makes more sense

  8. #8
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by acbmse View Post
    So, I guess my question is, what is the difference in using 'new Array' in the var and not using it?

    (new Array) == ([])

    just as

    (new Object) == ({})

  9. #9
    Join Date
    May 2011
    Posts
    8
    Thanks for your help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles