Error while trying to use Inheritance


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/objects-ii/3/1?curriculum_id=506324b3a7dffd00020bf661#

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
Error message:
It looks like your Animal.prototype.sayName method does not properly log to the console ‘Hi my name is [name]’ where [name] is the name of the Animal

```

// create your Animal class here
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}

var Lion = new Animal(“King”, 4);
// create the sayName method for Animal
Animal.prototype.sayName = function() {
console.log("Hi my name is " + name);
};

// provided code to test above constructor and method
var penguin = new Animal(“Captain Cook”, 2);
penguin.sayName();

<do not remove the three backticks above>

#2

Hi @rajakhader,

Here:

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

You’re missing this. for the name in the console.log above.

This is because you’re referring to key name of Animal class in function Animal(name, numLegs) {......} .

Hope it helps :slight_smile:


#3

Yes it worked.

Thank you


#4

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