Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

questions about arrays

Status
Not open for further replies.

estesflyer

Programmer
Dec 19, 2000
284
US
hello, i was doing fine learning javascript, until i came upon arrays, which i am still stumbling around trying to figure out how to use them! could somebody plz explain to me what the importance of arrays, and when and why and how to use em? i would be EXTREMELY greatful if someone can do that for me. thx!
 
they are lists. they are good for when you want to use things in a list context. umm, they are wonderful things. for example, there might be a time when you dont know exactly how many variables you are going to have, so you stick them all in an array, then cycle through it with a for loop, to access them all. adam@aauser.com
 
hey jared, i have already looked at numerous tutorials, including my book, SAMS teach yourself javascript in 24 hours. And i still do not understand how you can use arrays. i mean, i understand that you can make names[0] and birthdays[0] and try to do stuff like that, etc.. and for validating a form, and for, well, lots of things... anyway, i started out to learn javascript coz i wanted to be more prepared for the big stuff (c++, qbasic, php, cold fusion, etc...) than i could be if i didnt learn something easy (many people tell me that javascript IS NOT easy, but i donno, seams kinda easy to me, sept for arrays... but maybe its just where im trying to get the info from, i donno....) but i cant give up now, i dotn care if it takes me a month to understand how to use arrays alone, but, prolly after that, i will give up. :) i just wanna know the basics, so i will be able to edit, etc... other scripts in the future. :)
 
> seams kinda easy to me, sept for arrays
> i dotn care if it takes me a month to understand how to use arrays

Way back :) when I was in school one of the first pre-requisites for a CS degree was 'Programming Logic and Design'. This course was not 'language' based but focused on common constructs in almost all programming languages like flow of control and arrays, sorting algorithms, etc.

The notion was that you need understanding of some fundamental concepts and constructs to proceed successfully in a software development career. It still seems like a reasonable notion to me.

"But, that's just my opinion... I could be wrong".
-pete
 
One way to use arrays is to store related objects so that you can iterate through them later. Maybe a simple example will help:

<script language=&quot;javascript1.1&quot;>
<!--

// Creates a new person
function buildPerson(name,age,sex)
{
this.name=name;
this.age=age;
this.sex=sex;
}

// Adds a person to the list specified in the first parameter
function addPersonToList(list,name,age,sex)
{
list[list.length]=new buildPerson(name,age,sex);
}

// Display the people
function listPeople(title,list)
{
var result=title + &quot;\n---------------------------\n&quot;;

for (var i in list) {
result+=list.name + &quot; &quot; + list.age + &quot; &quot; + list.sex + &quot;\n&quot;;
}

return result;
}

// Create an empty list to populate
var people=new Array();

// Add some random people to the list
addPersonToList(people,&quot;Ralph&quot;,10,&quot;M&quot;);
addPersonToList(people,&quot;Jezebel&quot;,25,&quot;F&quot;);
addPersonToList(people,&quot;Johnson&quot;,15,&quot;M&quot;);
addPersonToList(people,&quot;Gretchen&quot;,60,&quot;F&quot;);

// Display the list
alert(listPeople(&quot;Invisible Citizens&quot;,people));
//-->
</script>

This is trivial, but you could write other functions and add other members to the person object to make it do more interesting things. You could store many people in your list and then call a function like listPeople that takes just a few lines of code to perform an operation(s) on all the people in the list.

As palbano mentioned, arrays are a generalized programming construct, so you'll definitely need to get a good grasp on them if you want to move to other languages in the future.

I hope this helps and I haven't confused things :)

Russ
bobbitts@hotmail.com
 
I have to agree with palbano. Go out and get yourself a Data Structures and Algorithms textbook... it'll go further in your quest to be a programmer than any tutorial on a single language. BTW, I learned with Pascal and found that to be a good learning language, especially since most pseudocode looks like Pascal ;-)
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
hey! thanks a lot! I understand what you guys are telling me, and i will take you up on your advice, unfortunately, i was not able to understand what that script was supposed to do exactly rbobbit, but what i do understand is that i must get a Data Structures and Algorithms textbook, if you know of any good books, or whatever, plz let me know, btw, these books, are they understandible, or does it take like, a rocket scientist to understand? I try to start out w/ the little things before I go onto the bigger things in life, so I say, always start out simple. :)

thanks for the advice!

- Rusty
 
Well, you could go to your favorite university website and find the CS department. I'm sure there will be class pages available, including Freshmen Data Structures (or equivalent) where the professor will list required and suggested textbooks. You can usually buy them at amazon.com or barnesandnoble.com. Generally, these introductory textbooks are written for beginners and you need not know how to program to understand them.

Here's my alma mater: Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top