1

I have this function

    function getStudents() {
                    var students = [];
                    students[0] = {name: "Anna", mark: 65, sex: "female"};
                    students[1] = {name: "James", mark:33, sex: "male"};
                    students[2] = {name: "William", mark: 87, sex: "male"};
                    students[3] = {name: "Jane", mark: 72, sex: "female"};
                    students[4] = {name: "Rikki", mark: 60, sex: "male"};
                    students[5] = {name: "Angela", mark: 58, sex: "female"};    
            }

And then in the body i try to do this:

        var students = getStudents();

        var referrals = ["James", "Angela"];
                for(var i = 0; i < students.length; i++){
        var pTag ="<p>";

However, as soon as I try to go through the loop it tells me that it cannot do .length of an undefined variable, but I thought I already defined it by calling the function and assigning it to a variable?

2
  • 6
    You forgot to return array Commented Feb 28, 2017 at 11:09
  • What other guys say. Also, your var students = [] in the function has no effect on the outer world. That var exists only within the function. Commented Feb 28, 2017 at 11:10

4 Answers 4

3

Change your function for this code. The problem is that you are not returning the variable students and when you are out of the scope that variable does not exist anymore. With the return you will assign it to the variable when you are calling the function.

function getStudents() {
                var students = [];
                students[0] = {name: "Anna", mark: 65, sex: "female"};
                students[1] = {name: "James", mark:33, sex: "male"};
                students[2] = {name: "William", mark: 87, sex: "male"};
                students[3] = {name: "Jane", mark: 72, sex: "female"};
                students[4] = {name: "Rikki", mark: 60, sex: "male"};
                students[5] = {name: "Angela", mark: 58, sex: "female"};    
                return students;            
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't post incomplete answers and then edit them to make them complete. Post a complete answer in the first place.
1

You should return the variable

function getStudents() {
    var students = [];
    students[0] = {name: "Anna", mark: 65, sex: "female"};
    students[1] = {name: "James", mark:33, sex: "male"};
    students[2] = {name: "William", mark: 87, sex: "male"};
    students[3] = {name: "Jane", mark: 72, sex: "female"};
    students[4] = {name: "Rikki", mark: 60, sex: "male"};
    students[5] = {name: "Angela", mark: 58, sex: "female"};    
    return students;
}

Comments

0

You need to return the students array at the end of the function for the variable to be assigned the array from getStudents:

function getStudents() {
     var students = [];
     students[0] = {name: "Anna", mark: 65, sex: "female"};
     students[1] = {name: "James", mark:33, sex: "male"};
     students[2] = {name: "William", mark: 87, sex: "male"};
     students[3] = {name: "Jane", mark: 72, sex: "female"};
     students[4] = {name: "Rikki", mark: 60, sex: "male"};
     students[5] = {name: "Angela", mark: 58, sex: "female"}; 
     return students;   
}

Otherwise where you say var students = getStudents(); it's simply doing a function call and not assigning the array.

Comments

-1

You cannot request the length of an object you did not return in the function call that creates the object. You are assigning nothing to the students variable as the getStudents() function does not return a value or object thus the error you are getting.

Use developer console on the jsfiddle below to see this demonstrated

jsFiddle

function getStudents() {
    var students = [];
  students[0] = {name: "Anna", mark: 65, sex: "female"};
  students[1] = {name: "James", mark:33, sex: "male"};
  students[2] = {name: "William", mark: 87, sex: "male"};
  students[3] = {name: "Jane", mark: 72, sex: "female"};
  students[4] = {name: "Rikki", mark: 60, sex: "male"};
  students[5] = {name: "Angela", mark: 58, sex: "female"};  

  return students;
}

var students = getStudents();
//var referrals = ["James", "Angela"];
console.log(students);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.