LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   returning an array from a function.. javascript (https://www.linuxquestions.org/questions/programming-9/returning-an-array-from-a-function-javascript-190593/)

sonesay 06-07-2004 03:58 AM

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.

sonesay 06-07-2004 05:28 AM

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?


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