30. Private Eye


#1

Hello, I am very stuck. Though I read the comments and see the solution is to remove this from the prototype, I don't understand why and want to learn versus copy and paste.

https://www.codecademy.com/courses/objects-ii/5/4?curriculum_id=506324b3a7dffd00020bf661#

function StudentReport() {
this.grade1 = 4;
this.grade2 = 2;
this.grade3 = 1;
this.getGPA = function() {
return (this.grade1 + this.grade2 + this.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());


#2

here:

this.grade1 = 4;
this.grade2 = 2;
this.grade3 = 1;

removing this will make grade1, grade2 and grade3 private. Which means this properties can't be accessed directly like you try to do in your for loop


#3

But I thought that adding a method in the constructor made GPA private:

this.getGPA = function() {
return (this.grade1 + this.grade2 + this.grade3) / 3;
};

And from there I still don't understand the lesson.


#4

With the code below I get this message: Oops, try again. Make sure that getGPA returns the average of the three grades. It should be 2.3333333333333335 but yours returned NaN

'''
function StudentReport() {
var grade1 = 4;
var grade2 = 2;
var grade3 = 1;
this.getGPA = function() {
return (this.grade1 + this.grade2 + this.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());
'''


#5

we make our grade1, grade2 and grade3 properties private by changing this into var. This means we can't access grade1 directly:

var myStudentReport = new StudentReport();
myStudentReport.grade1 # won't work, grade1 is now private property)

so we make a method (getGPA ) through which we can access our private properties.

the method is public. Using the public getGPA method, we access private properties)


#6

OK, I got it right, but nothing printed. Why not?

Is this correct: By removing the 'this' changed the objects from public to private. Then I needed to chage the objects in the method.

'''

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());
'''


#7

because that is the point? You made the properties private, so now you can't access them, so they won't be printed


#8

Oh! Thank you! I assumed because the colsole.log code was there I was supposed to print something.


#9

the for in loop is there to show that you can't access private properties


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.