0

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);
    }
7
  • 1
    Why on earth do you have this line i = i % this.students.length;? Commented Dec 20, 2013 at 14:15
  • And why do you have a nested loop over the students array inside a loop over the students array? Commented Dec 20, 2013 at 14:17
  • I did this so I could console log the first object, then the second object without having more then one set of console logs. Is there a better way to do this? Commented Dec 20, 2013 at 14:19
  • I am just learning so i know my code is bad. I am just looking to improve. Commented Dec 20, 2013 at 14:20
  • Well you should decide whether the grade point array is called "gpa" or "gpa1". It can't be both :) Commented Dec 20, 2013 at 14:20

3 Answers 3

1

This should be easy, loop over the students, then loop over their GPA score, add them up and divide by the total number of points:

testing: function () {
    var total = 0;
    var numPoints = 0;
    for (var i = 0; i < this.students.length; i++) {
        var thisStudentsTotal = 0;
        for (var j = 0; j < this.students[i].gpa.length; j++) {
            total += this.students[i].gpa[j];
            thisStudentsTotal += this.students[i].gpa[j];
            numPoints++;
        }
        // This is the average for just this student
        this.students[i].myAvg = thisStudentsTotal / this.students[i].gpa.length;
    }
    // This is the average for every student
    var theAverage = total / numPoints;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Does the OP want the average of all of the students, or the average for each individual student?
@newfurniturey: beats me. But I've added both now.
1

You have a starting-point to get the average, however, it looks like you're leaving it as the "total" instead:

this.average = this.gpa1[0] + this.gpa1[1] + this.gpa1[2];

Now, assuming that there will only ever be three grades, we could make just a small change:

this.average = (this.gpa1[0] + this.gpa1[1] + this.gpa1[2]) / 3.0;

If you want a more dynamic average, though it's probably not going to be the case, you could perform the calculations in a loop:

for (var avg = 0; avg < this.gpa1.length; avg++) {
    this.average += this.gpa1[avg];
}
this.average = this.average / this.gpa1.length;

Both of my solutions above are for calculating the average for each individual student; if you need to calculate a total average for all students, adjustments to the loop above should suffice but for a more precise example check out @MattBurland's answer.

Comments

0

I would be inclined to add the GPA average to the object before you loop over it. That way you can reuse it in other parts of the program without duplicating code.

Here I've used the Array.map and Array.reduce methods to add the average (to two decimal places) to each student object, simply because I think it's neater.

this.students.map(function (el) {
  el.avg = (el.gpa.reduce(function (p, c) {
    return p + c;
  }) / el.gpa.length).toFixed(2);
  return el;
});

And now you just loop over the objects.

for (var i = 0, l = this.students.length; i < l; i++) {
  var student = this.students[i];
  console.log("Name: " + student.name);
  // etc
}

I've added a fiddle which includes your code, corrected slightly where I've seen an error. I've separated the getAverageGpa code into a new function.

1 Comment

Of course Array.map and Array.reduce both need to iterate through the collection so, essentially, you still have a nested loop.

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.