Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

JavaScript:Tutorial, Using Arrays

array

  • Please log in to reply
25 replies to this topic

#1 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 03 December 2006 - 07:52 AM

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:-

<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

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)

var msg = new Array("msg1","msg2","msg3","msg4","msg5","msg6","msg7","msg8");


And call it with something like this:-
<BODY onLoad="array_use()">

Explanation:-
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:-
Posted Image

Conclusion:-
As Always Feedback is welcome and the full source is attached!!

Attached Files


Edited by TcM, 26 June 2008 - 10:30 AM.

  • -1

#2 RikkoSuperb

RikkoSuperb

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 23 August 2007 - 06:03 AM

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.
  • 0

#3 Rohit

Rohit

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 24 June 2008 - 10:34 PM

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

#4 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 25 June 2008 - 12:02 AM

Welcome.

Thanks for positive comment.
  • 0

#5 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 25 June 2008 - 12:18 PM

Your code isn't particularly efficient. A shorter way to declare an array like that:

var msg = new Array("msg1","msg2","msg3","msg4","msg5","msg6","msg7","msg8");

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 25 June 2008 - 01:22 PM

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

#7 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 26 June 2008 - 08:08 AM

I'm just saying, it's an awful lot of code there, which could easily be simplified.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 26 June 2008 - 09:33 AM

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

#9 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 26 June 2008 - 09:34 AM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#10 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 26 June 2008 - 09:37 AM

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

alert(msg[i]);

  • 0

#11 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 26 June 2008 - 09:43 AM

Yeah, but it would be good to include both methods, to show people the different methods of doing it.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#12 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 26 June 2008 - 10:29 AM

Ok. I edited the first post, and added your code.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download