Jump to content

JavaScript:Tutorial, Using Arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
25 replies to this topic

#1
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
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.


#2
RikkoSuperb

RikkoSuperb

    Newbie

  • Members
  • PipPip
  • 20 posts
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.
RikkoSuperb

#3
Rohit

Rohit

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks to share this information,good information about how we can use array in <head> tag

#4
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Welcome.

Thanks for positive comment.

#5
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
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","msg7","msg8");
[/HIGHLIGHT]
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#6
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
These tutorials are not about efficiency, but about getting the basic thing and the concept of it into the readers mind.

#7
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I'm just saying, it's an awful lot of code there, which could easily be simplified.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#8
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Yeah, but makes it harder for someone trying to learn the concept of Arrays.

#9
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
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.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#10
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
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]);


#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Yeah, but it would be good to include both methods, to show people the different methods of doing it.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#12
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Ok. I edited the first post, and added your code.