30/30 for loop meaning?


#1

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

Question: Code is correct, but I do not understand the for statement. What does it mean that a variable [x, in this case] can or cannot equal a "function"? How come once the properties in object Student Report become private (by changing from this. to var) that the if condition in the for loop is no longer valid? Thanks in advance for any help.


#2

@ld000,

T h e - B a s i c s

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,
- a fullName property with property-key fullName and it's associated string value of "New York City"
- a mayor property with property-key mayor and it's associated string value of "Bill de Blasio"
- a population property with property-key population and it's associated number value 8000000
- a boroughs property with property-key boroughs and it's associated number value 5

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration's
iteration-1 var x = "fullName";
iteration-2 var x = "mayor";
iteration-3 var x = "population";
iteration-4 var x = "boroughs";
As they want you to display the property-key and NOT it's associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );


#3

@ld000,
With

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

you created a so-called Object Class constructor StudentReport()
and creating a so-called Instance of this Class of Objects
using the new keyword like

var myStudentReport = new StudentReport();

and then using the variable myStudentReport as an argument for the console.log() Method
like

console.log( myStudentReport );

you will get the [output]

{ getGPA: [Function] }

and you will NOT see the privat properties.

Now if you would add some extra console.log()'s
in your FOR-IN loop
you would understand
that the Original FOR-IN loop code would NOT produce an output
as the only property pubicly-accessable would be the Method/Function getGPA.

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