Prints Characters Instead of Decimal


#1



The getGPA function's math formula should return a float. For some reason, it seems to return characters instead. When that function gets called, it prints NaN in the place of the GPA. The error message points out, "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());


#2

These variables are undefined. undefined + undefined == NaN

Since the variables are private to the class, they do not have a context on the outside. We refer to them directly.

(grade1 + grade2 + grade3)

#3

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