Lesson passes even though code does not function as expected


#1
function Animal(param1,param2){
    this.name=param1;
    this.numLegs=param2;
};



// method for var objectName = new animal(arg,arg);
Animal.prototype.sayName=function(param){
    console.log("Hi, my name is "+param.name);
};



// animalClass object
    var penguin=new Animal("sqwakPaddleFlap", 2);



penguin.sayName();

replacing my console.print line with console.log("Hi, my name is "+this.name); causes the lesson to fail and tell me that my functions fails to properly log the string to the console. However leaving it as it is the lesson passes me but the console prints an error telling me that param is undefined, instead of printing out the string I expected. What gives?


#2

according to hoe you defined the sayName method here

it takes in one argument but in your call i see no argument passed


#3

So I'm supposed to call:
objectName.universalMethod(and-then-also-specify-value-data-specific-to-the-object);??

I'm sorry, I seem to be having trouble following these lessons. Why would I have penguin.sayName("squawk");? Wouldn't that defeat the purpose of making a universal method which any animal could use to automatically say its own name?

Also, I tried what you said, and you were right, that got the string to print :smile:
But still not as expected... I tried penguin.sayName("squawk");
This is what the console returned: Hi, my name is undefined X.X


#4

ohh i see now the error is here

should not be param.name but rather this.name

modify the method to this

Animal.prototype.sayName=function(){
    console.log("Hi, my name is "+this.name);
};

Now you can call it like this

penguin.sayName();

I missed that


#5

X.X lol I just figured it out myself. That was actually the first thing I tried.
I've seen reports on here where some people say they have to refresh the page before the program that screens user code functions properly. In my case (I'm discovering) I have to actually load the url in a fresh browser window or tab. Yeesh. I tell you though, spend twelve hours trying to get the wrong code to work after giving up on the right one only to discover the problem was a glitch --you don't actually end up absorbing the lesson...