LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-07-2004, 03:58 AM   #1
sonesay
Member
 
Registered: May 2004
Location: AUCKLAND
Distribution: Fedora core 2 , RH9
Posts: 64

Rep: Reputation: 15
returning an array from a function.. javascript


Hey I'm making an array and trying to return it from a function.. i dont know how... can someone help me please....

Code:
<html>
<head>
<title> </title>

<script language="javascript">

function uni(name,city,studNum) {

	this.name = name;
	this.city = city;
	this.studNum = studNum;
}

function initialise() {

	var University = new Array();
	
	University[0] = new uni("Victoria Univeristy","Wellington","16000");
	University[1] = new uni("University of Canterbury","Christchurch","12500");
	University[2] = new uni("Otago Univeristy","Dunedin","17000");
	University[3] = new uni("Auckland Univeristy","Auckland","26000");
	
	return University;	
}

function display() {

	for (var i=0; i < University.length; i++) {
		document.write("Name: "+University[i].name+"<br>");
		document.write("City: "+University[i].city+"<br>");
		document.write("Enrollment: "+University[i].studNum+"<br>");
	}

}

</script>
</head>
<body>
<script language="javascript">

initialise();

display();


</script>
</body>
</html>
I'm getting an error at the for loop when I'm usiing "University.length" it says "University" is not defined.. I think my array function is not returing the array.. i cant figure it out.
 
Old 06-07-2004, 05:28 AM   #2
sonesay
Member
 
Registered: May 2004
Location: AUCKLAND
Distribution: Fedora core 2 , RH9
Posts: 64

Original Poster
Rep: Reputation: 15
ok i've got ti to work but still unsure about correct way of returning and using arrays from a function.. heres what i changed.
Code:
<html>
<head>
<title>Univeristy Arrays</title>
<script language="javascript">

function uni(name,city,studNum) {

	this.name = name;
	this.city = city;
	this.studNum = studNum;
}

function initialise() {

	var University = new Array();
	
	University[0] = new uni("Victoria Univeristy","Wellington","16000");
	University[1] = new uni("University of Canterbury","Christchurch","12500");
	University[2] = new uni("Otago Univeristy","Dunedin","17000");
	University[3] = new uni("Auckland Univeristy","Auckland","26000");
	
	return University;	
}

function display(thisArray) {

	for (var i=0; i < thisArray.length; i++) {
		document.write("Name: "+thisArray[i].name+"<br>");
		document.write("City: "+thisArray[i].city+"<br>");
		document.write("Enrollment: "+thisArray[i].studNum+"<br><br>");
	}

}
</script>
</head>
<body>
<script language="javascript">

var x;

x = initialise();

display(x);
</script>
</body>
</html>
Is this a good/correct way to catch a return from an array and use it? I've only been learning to program for a few months now.. is it the similar in any other language to do what i am doing?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
telephone number array in javascript esteeven Programming 2 06-08-2005 04:02 PM
passing php string to javascript function djgerbavore Programming 2 03-01-2005 11:34 AM
expandIt function in JavaScript: COOL menus xbaez Programming 0 02-13-2005 06:36 PM
sending pointer array to function marek Programming 4 04-15-2004 04:46 PM
c++ function & array question shaggy112 Programming 1 05-24-2001 10:41 PM


All times are GMT -5. The time now is 07:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration