+ Reply to Thread
Page 1 of 3
1 2 3 LastLast
Results 1 to 10 of 26

Thread: JavaScript:Tutorial, Using Arrays

  1. #1
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    JavaScript:Tutorial, Using Arrays

    Introduction:-
    Usually this is a difficult thing when it comes to programming and usually people get mixed up with arrays.

    Solution:-
    Make this Function between the <head> tags:-

    Code:
    <SCRIPT language="JavaScript">
    
    function array_use()
    {
    
    var msg= new Array(8)
    msg[0]="Msg1";
    msg[1]="Msg2";
    msg[2]="Msg3";
    msg[3]="Msg4";
    msg[4]="Msg5";
    msg[5]="Msg6";
    msg[6]="Msg7";
    msg[7]="Msg8";
    
    var i=0;
    
    
    
    for (i=0; i<8; i++)
    
      {
         alert(msg[i]);
      }
    
    }
    
    </SCRIPT>
    Instead of

    Code:
    var msg= new Array(8)
    msg[0]="Msg1";
    msg[1]="Msg2";
    msg[2]="Msg3";
    msg[3]="Msg4";
    msg[4]="Msg5";
    msg[5]="Msg6";
    msg[6]="Msg7";
    msg[7]="Msg8";
    You can make (Untested)

    Code:
    var msg = new Array("msg1","msg2","msg3","msg4","msg5","msg6","msg7","msg8");
    And call it with something like this:-
    Code:
    <BODY onLoad="array_use()">
    Explanation:-
    Code:
    for (i=0; i<8; i++)
    
      {
         alert(msg[i]);
      }
    This is a Loop that will display a msg and increase i by one every time and it will go on until i<8

    A Preview:-


    Conclusion:-
    As Always Feedback is welcome and the full source is attached!!
    Attached Files
    Last edited by TcM; 06-26-2008 at 01:30 PM.

  2. #2
    Newbie RikkoSuperb is an unknown quantity at this point RikkoSuperb's Avatar
    Join Date
    Aug 2007
    Location
    holywood, county down, northern ireland
    Age
    28
    Posts
    20
    What else can you do with arrays? I have used them in coursework but the examples are lame - like amount of books borrowed from a library that are printed to the document for the user to view. wow. ground breaking stuff? methinks not.

  3. #3
    Newbie Rohit is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    3

    Re: JavaScript:Tutorial, Using Arrays

    Thanks to share this information,good information about how we can use array in <head> tag

  4. #4
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: JavaScript:Tutorial, Using Arrays

    Welcome.

    Thanks for positive comment.

  5. #5
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: JavaScript:Tutorial, Using Arrays

    Your code isn't particularly efficient. A shorter way to declare an array like that:
    [HIGHLIGHT=javascript]
    var msg = new Array("msg1","msg2","msg3","msg4","msg5","msg6","m sg7","msg8");
    [/HIGHLIGHT]

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  6. #6
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: JavaScript:Tutorial, Using Arrays

    These tutorials are not about efficiency, but about getting the basic thing and the concept of it into the readers mind.

  7. #7
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: JavaScript:Tutorial, Using Arrays

    I'm just saying, it's an awful lot of code there, which could easily be simplified.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  8. #8
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: JavaScript:Tutorial, Using Arrays

    Yeah, but makes it harder for someone trying to learn the concept of Arrays.

  9. #9
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: JavaScript:Tutorial, Using Arrays

    I can't see how it is harder - if anything it's easier, because you don't need to concern yourself with array indices - simply define the elements.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #10
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: JavaScript:Tutorial, Using Arrays

    But as Arrays start with 0 and not with 1, if you don't make those indices, they might get confused. And they will not understand the concept of

    Code:
    alert(msg[i]);

+ Reply to Thread
Page 1 of 3
1 2 3 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Python arrays...
    By Sir_Rimo in forum Python
    Replies: 3
    Last Post: 06-20-2007, 08:54 AM
  2. Arrays
    By clookid in forum PHP Tutorials
    Replies: 1
    Last Post: 01-11-2007, 08:30 PM
  3. Are arrays sort of like matrices?
    By Sionofdarkness in forum Java Help
    Replies: 5
    Last Post: 08-21-2006, 02:30 PM
  4. Arrays
    By Sionofdarkness in forum C and C++
    Replies: 5
    Last Post: 07-26-2006, 05:35 PM
  5. Arrays in NET 2.0 or CLI Managed C++
    By Void in forum Managed C++
    Replies: 1
    Last Post: 07-18-2006, 07:57 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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