Whats wrong with this code


#1


https://www.codecademy.com/en/courses/objects-ii/3/1?curriculum_id=506324b3a7dffd00020bf661#


Oops, try again. 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;
}


// create the sayName method for Animal
Animal.prototype.sayName = function() {
    console.log("Hi my name is " + Animal.name);
};




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


#2

to get access to instance variable use this.name, not Animal.name


#3

why Animal.name is not working here for accessing value?


#4

because Animal is an object. objects:

function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}

are just blue prints, they describe something (in this case an animal)

instances:

var penguin = new Animal("Captain Cook", 2);

are actually animals, in this case a penguin.

a blue print of a penguin doesn't have a penguin name


#5

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