30/30 privatte eye


#1

function StudentReport(grade1, grade2, grade3) {
var grade1 = 4;
var grade2 = 2;
var grade3 = 1;
this.getGPA = function() {
var GPA = (this.grade1 + this.grade2 + this.grade3) / 3;
};
}

var myStudentReport = new StudentReport();

for(var x in myStudentReport) {

}

console.log("Your overall GPA is " + myStudentReport.getGPA());

it says

Oops, try again. Make sure that getGPA returns the average of the three grades. It should be 2.3333333333333335 but yours returned undefined


#2

Change var GPA = (this.grade1 + this.grade2 + this.grade3) / 3;
Replace it with return (grade1 + grade2 + grade3) / 3;

You get undefined because you are not returning anything.
And no need for this on your addition since you have already defined your variables above which are in the scope of the getGPA function.


#3

As you have already defined values for grades so you now don't need to write this.grade instead of this use simply ..
var GPA = (grade1 + grade2 + grade3) / 3;

As you were using this.grade so it was showing values as NaN..


#4

@mr_cheese_47

You're on the right track, The goal of the exercise was:
1) Prevent grade values from being printed by making them private.
2) Keep the getGPA method working by keeping it as a public method.
3) By doing all of the above, you are now able to display GPA but NOT show the grades.

So you've done step 1 by changing the StudentReport public properties into private (i.e. this.grade1 to var grade 1).

Now step 2 is to keep the** this.getGPA** method working. Since we've changed the properties by declaring them simply as var grade1, they can now be called inside the return( ) as grade1,.. etc.(simply by removing (this.). This will allow the getGPA method to access the declared private properties only because it is in the same class.

Hope this helps.


#5

function StudentReport() {
var grade1 = 4;
var grade2 = 2;
var grade3 = 1;
this.getGPA = function() {
return (grade1 + grade2 + grade3) / 3;
};
}

var myStudentReport = new StudentReport();

for(var x in myStudentReport) {
if(typeof myStudentReport[x] !== "function") {
console.log("Muahaha! " + myStudentReport[x]);
}
}

console.log("Your overall GPA is " + myStudentReport.getGPA());