I am looking to to loop through an array of objects in my prototype function. After I loop I want to get the average of the gpa array and then console log the name, address, gpa and average gpa. I am able to do all but the average. I want this to all run together within the testing function. Thanks for all the help.
var students = [{
name: "Walker",
address: {
street: "123 South Drive",
city: 'Sarasota',
state: 'FL'
},
gpa: [3.0, 3.4, 3.8]
}, {
name: "Christian",
address: {
street: "5601 Pebble Beach Ln",
city: 'Sacromento',
state: 'CA'
},
gpa: [2.5, 3.6, 3.8]
}];
var stud = Students(students);
stud.testing();
constuctor.js:
var Students = function (students) {
return new Students.prototype.init(students);
}
Students.prototype = {
init: function (students) {
this.students = students;
},
testing: function () {
for (i = 0; i < this.students.length; i++) {
i = i % this.students.length;
for (j = 0; j < this.students.length; j++) {
this.gpa1 = this.students[j].gpa;
this.len = this.students[j].gpa.length;
//console.log(this.gpa1);
this.average = this.gpa1[0] + this.gpa1[1] + this.gpa1[2];
this.res = this.average / this.len;
console.log(this.res);
}
console.log("Name: " + this.students[i].name);
console.log("Address: " + this.students[i].address.street + ' ' + this.students[i].address.city + ' ' + this.students[i].address.state);
console.log("GPA: " + this.students[i].gpa);
console.log("Average: " + this.gpa1);
}
},
addData: function (student3) {
this.students.push(student3);
}
i = i % this.students.length;?