16. It's All in the Genes

I Don’t Know What is the problem in my code

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

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

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

Try this here

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

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

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

or you can test it HERE

5 Likes

you must use this.name , not just “name”

1 Like

Also, codecademy requires that you use improper grammar. It should be ‘Hi, my name’ not ‘Hi my name’.

8 Likes

I used the exact same code and nothing prints out to the console. Anticipated thanks

Ok. After refreshing the page, it did output the result.

You should put space between "Hi my name is " and this.name to work it properly.
You can do this: "Hi my name is " + " " + this.name

1 Like

The space after the is handles that

Hi. My code prints correctly to the console, but I get this message saying my method does not properly log to the console. This is my code:

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

I have the same problem…Does anyone have an idea?

I realized I included a comma after Hi. But the author didn’t specify one. This is why the console printed out the string, but did not fully accept the code. Hope that helps @jt1

2 Likes

Thanks! That worked for me.

I’m curious about this question as well since my code is similar to the author’s. I have the right answer to the exercise, but why is “above string here” + name not acceptable? Is there a good reason or is Codeacademy just being finicky?

Look at my code …please help me// 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 " + this.name);
};
// provided code to test above constructor and method
var penguin = new Animal(“Captain Cook”, 2);
penguin.sayName();

output: Hi my name is Captain Cook.
BUT THE PROBLEM IS I’M STILL GETTING THIS ERROR MESSAGE
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
BECAUSE OF THIS ERROR ICAN’T GO FURTHER IN THIS COURSE.

After refreshing my browser, this is the code I ran and it worked:

// 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 " + this.name);
};

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

penguin.sayName();

Hi can anyone help when i submit it comes up with “SyntaxError: missing ) after argument list”
I’ve looked through it but can’t seem to figure out what i’m missing i’m hoping some fresh eyes will help

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" + " " this.name);
    };


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

check this line you have an error there

Hi my code is correct and i 'm getting the error:
Oops, try again. It looks like your Animal constructor does not take in two arguments and save them as ‘this.name’ and ‘this.numLegs’ respectively.

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 "+this.name);
};

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

Yea, I had trouble with this because I didn’t have space between my this.name. Thanks for that extra comment!

console.log("Hi my anme is " + name);
>>Should be console.log("Hi my name is " +this.name);