Objects II - Fun With Functions


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?


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


speak should be like this:

function Person(job, married, speak) {
this.job = job;
this.married = married;
 function : speak(){

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


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


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

speak : function(){

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


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


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

It's supposed to be:

    this.speak = function() {


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


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


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