3 Literally Speaking. Passed but error returned?


#1

Hey so I had a question about this.

var james = {
    job: "programmer",
    married: false,
    speak: function(  ) {
        console.log("Hello, I am feeling " + speak);
    }
};

james.speak("great");
james.speak("just okay");

It worked, but this was returned after running the code
"ReferenceError: 'speak' is undefined"

Any reason why?


#2

I am also receiving the same error in Lesson 4

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

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

// change james' job to "super programmer" here
james.job = "super programmer"

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

#3

Hi,

console.log("Hello, I am feeling " + speak);

In this line, JS will look for a variable named speak. To refer to properties of james object, you need to use this keyword, do you know how to? :wink:


#4

so what is the answer to that problem ???


#5

Well, when you call the speak function, you pass it the string "great". However, the function doesn't have any parameters (things in parenthesis). Try giving a parameter to the function and using that to output the string.


#6

In this lecture, you are been shown how to add methods in literal notation. This is your method function:

speak: function( ) {
console.log("Hello, I am feeling " + speak);

Your code is telling the method function to print "Hello, I am feeling " + the method function (speak), which is confusing.

You should rather add a parameter to the method function(speak), and then to the console.log() since the call for the function:

james.speak("great");
james.speak("just okay");

shows the james object and the speak method will be printed on screen.


#7

Right but what ever I do in those lessons i receive the error. they pass and i've moved on but still an error shows .


#8

If you tried any new code, please post it here for the community to inspect and help you debug. Do let us know if the earlier answers did not help you understand your current code's problem :slightly_smiling:


#9

var james = {
job: "programmer",
married: false,
speak: function(mood) {
console.log("Hello, I am feeling" + this.speak());
console.log("Hello, I am feeling" + this.speak());
}
};

james.speak("great");
james.speak("just okay");

I get RangeError: Maximum call stack size exceeded


#10

You should add the (mood) parameter to your console.log() statement instead of the method function(speak), because as you can see,

james.speak("great");
james.speak("just okay");

these two calls are inputting a mood into the speak method.


#11

When you call, james.speak, the function internally itself calls james.speak (which is the same as this.speak):

speak: function(mood) {
    console.log("Hello, I am feeling" + this.speak());
    console.log("Hello, I am feeling" + this.speak());
}

which again calls itself and this keeps on going as a self-calling endless cycle (infinite recursion).


#12

speak: function( ) {
this.mood = mood;
console.log("Hello, I am feeling" + speak);
}
};