"Private Eye"


#1

Hello! My code below seems to be working, since no grades are printed to the console, and the GPA calculation is not affected.

But it says "Oops, try again. Make sure that grade1 grade2 and grade3 are all private!" I thought I already made it private in my code? Since the for loop targets only what are NOT functions, and var grade 1,2,3 makes it private.

Where did I do wrong?
Thanks!

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

Looks good as for me and it works in Chrome console. Try rename functions: getGrade1, getGrade2, getGrade3.


#3

Hey thanks for the advice! But I just realized I was actually taking a huge unnecessary d├ętour...actually I could've written it in a more concise way as following:

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

Hahaha!


#4

You have wrong syntax in return statement. Should be:

return (grade1 + grade2 + grade3)/3;

#5

Oh didn't notice it thanks for pointing out!


#6

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