It's In The Genes - Can't console the correct thing


#1

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

I can't console the correct thing.

Please help me find the error in this code.


// 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 [name]");
}



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


#2

you made name accessible inside the object thanks to this line:

this.name = name;

so you need to use this.name, also you need to join the string and variable together:

var example = "how to join the strings";
console.log("this is " + example);

you need to the same way to join your string and the variable (this.name)


#3

Hi, thanks for the correction, but I didn't get what you said in the first paragraph. Is it wrong to use this.name = name ?


#4

no, it is good you do this:

this.name = name

it means you can access the name through the this object, it was just just information that you needed to this.name in your sayName method.


#5

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