Can I see your references, not printing


#1

Im really curious as to why this code is letting me pass to the next question, but isnt printing anything to the console.

var james = {
    job: "programmer",
    married: false,
    sayJob: function() {
        // complete this method
      console.log("Hi, I work as a " + job + " ");
    }
};

// james' first job
james.sayJob();

// change james' job to "super programmer" here
james.sayJob("super programmer");

// james' second job
james.sayJob("lawyer");


#2

here:

console.log("Hi, I work as a " + job + " ");

job is undefined, to access member variable (job is a member variable) use this.memberVariableName

here:

james.sayJob("super programmer");

you call the sayJob method, you don't update james his job. Also, the method has no parameters, so it shouldn't have any arguments

This code shouldn't have passed, you manage to create a corner case in which it does pass


#3

This sayJob method should has parameter to log it value in console not property job:

sayJob: function(job) {
   // complete this method
   console.log("Hi, I work as a " + job + " ");
}

Now you can call the method:

// james' first job
james.sayJob(); // logs programmer

// change james' job to "super programmer" here
james.sayJob("super programmer"); // logs super programmer

// james' second job
james.sayJob("lawyer"); // logs lawyer

Note: job property and job parameter in sayJob method are not the same things. So, if you want change property you need to add set method which can rewrite this property:

var james = {
    job: "programmer",
    married: false,
    setJob: function(newJob) {
        this.job = newJob
    },
    sayJob: function() {
        // complete this method
        console.log("Hi, I work as a " + this.job + " ");
    }
};

In this case sayJob method logs job's (property) current value.


#5

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