Objects II - Fun With Functions


#1

Here are the instructions: Add a speak method to the Person constructor. Whenever speak is called, it should print "Hello!" to the console.

Here is the code:
function Person(job, married, speak) {
this.job = job;
this.married = married;
this.speak = "Hello!";
}

var user = new Person("Codecademy Student",false);
console.log (user.speak);

Even though this returns "Hello!" as it should, I keep getting the following error message:
Oops, try again. Make sure you added a speak method to the Person constructor

Anyone have any suggestions?


#2

They say that speak should be a method. Yours looks like a string to me.


#3

speak should be like this:

function Person(job, married, speak) {
this.job = job;
this.married = married;
 function : speak(){
console.log("Hello")
}
}

to make it a method. Not your original code. Hope this helped!


#4

sorry this does not work. It starts off not liking the " : " after function. It just continues to be difficult with every change I make.


#5

@amanuel2, I believe what you were trying to do was a little backwards,

speak : function(){
console.log("Hello");
|

I will have to see if this is actually what the code is expecting of you, but try that for the moment.


#6

You nailed it. Thanks. I had to walk away from this one due to frustration. Now it seems so simple.


#7

No I was incorrect, you only use that for functions in an Object,

It's supposed to be:

    this.speak = function() {
    	console.log("Hello!");
    };

#8

Here is the code I entered and it worked:
this.speak = function() {
console.log ("Hello!");


#9

Sorry i was in a rush, just saying that is the old javascript. They updated it.


#10

exactly the same code is not working... did it work when u submitted?


#13