Hi! I need some help!

I’m trying to push all created objects into an array, but I get the error “Can not read property push ()”

var Student = function(name,surName,rating){
    this.name = name;
    this.surName = surName;
    this.rating = rating;
    this.fullName = function(){
        console.log(this.name + " " + this.surName +" "+"Середній бал"+" "+ this.avarage(this.rating));
    };
    this.avarage = function(arr){
        var sum = 0;
        arr.forEach(function(element,index){
            sum += Number(element)/arr.length;
        });
    return sum.toFixed(2);
    };
    Student.listStudent.push({...arguments});   // .  **_In this place <======I don't understand why ?
    this.getAll = function(){
        return Student.listStudent;
    };
};
var listStudent = [,];
var showAllStudent = function(){
    
};
var showBestStudent = function(){

};
var std = new Student('Ivan','Doshenko',[3,4,5,4,2]);
var std2 = new Student('Boris','Gabenko',[5,4,3,4,5,4]);
std2.fullName();
std.fullName();
console.log(Student.listStudent);

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

You refer to Student.listStudent inside your Student function, but you never declare a property named listStudent inside the Student function. Since listStudent is declared outside the Student function, why not just declare listStudent as an array outside the Student function like:

var listStudent = [];

and then insdie the Student function you would push to it like:

listStudent.push({...arguments});

then your console.log statement would be:

console.log(listStudent);
1 Like