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

Thread: JavaScript:Tutorial, Using Arrays

  1. #1
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    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 Attached Files
    Last edited by TcM; 06-26-2008 at 11:30 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    RikkoSuperb's Avatar
    RikkoSuperb is offline Newbie
    Join Date
    Aug 2007
    Location
    holywood, county down, northern ireland
    Posts
    20
    Rep Power
    0
    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.

  4. #3
    Rohit is offline Newbie
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    Re: JavaScript:Tutorial, Using Arrays

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

  5. #4
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    Re: JavaScript:Tutorial, Using Arrays

    Welcome.

    Thanks for positive comment.

  6. #5
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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

  7. #6
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    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.

  8. #7
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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

  9. #8
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    Re: JavaScript:Tutorial, Using Arrays

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

  10. #9
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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

  11. #10
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    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 123 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Beginner Tutorial: arrays in ruby
    By mr mike in forum Ruby on RailsTutorials
    Replies: 0
    Last Post: 12-23-2010, 12:44 PM
  2. Strings and Arrays Tutorial
    By hodge-podge in forum Perl
    Replies: 2
    Last Post: 05-06-2010, 06:08 PM
  3. Javascript Arrays - need help
    By webcodez in forum PHP Development
    Replies: 0
    Last Post: 04-15-2010, 01:14 AM
  4. C# Tutorial: Arrays
    By CommittedC0der in forum CSharp Tutorials
    Replies: 6
    Last Post: 03-30-2010, 03:14 PM
  5. Java:Tutorial - Arrays
    By John in forum Java Tutorials
    Replies: 8
    Last Post: 07-02-2009, 12:28 AM

Tags for this Thread

Bookmarks

Posting Permissions

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