17/30 I don't know what's the problem with my code


#1

I don't know what's the problem with my code, can someone help me?

function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
// create a Penguin constructor here
var Penguin = new Animal(this.name,this.numLegs);

// create a sayName method for Penguins here
Penguin.prototype.sayName = function(){
console.log("Hi, my name is " + this.name);
}
// our test code
var theCaptain = new Penguin("Captain Cook", 2);
theCaptain.sayName();
Errors:
Oops, try again. Make sure you properly defined the Penguin constructor
TypeError: Cannot set property 'sayName' of undefined


#2

We need to declare a Penguin constructor. There is no inheritance in this lesson.

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

instead of,

var Penguin = new Animal(this.name,this.numLegs);

#3

Thanks a lot for your support, but I need some more help here:
I have changed the code and it's showing me another error:
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};

// create a Penguin constructor here
function Penguin(name, numLegs){
this.name = name;
this.numLegs = numLegs;
}

// create a sayName method for Penguins here
Penguin.prototype.sayName = function(){
return "Hi my name is "+this.name;
}

// our test code
var theCaptain = new Penguin("Captain Cook", 2);
theCaptain.sayName();

Error:
Oops, try again. It looks like your Penguin.prototype.sayName method doesn't return 'Hi my name is [name]' where [name] is the penguin's name


#4
Penguin.prototype.sayName = function(){
    return "Hi my name is "+this.name;
};

Try console.log(...) instead of return ...


#5

I tried it already and I tried it now and it's not working.
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};

// create a Penguin constructor here
function Penguin(name, numLegs){
this.name = name;
this.numLegs = numLegs;
}

// create a sayName method for Penguins here
Penguin.prototype.sayName = function(){
return "Hi my name is "+this.name;
};

// our test code
var theCaptain = new Penguin("Captain Cook", 2);
theCaptain.sayName();

Error:
Oops, try again. It looks like your Penguin.prototype.sayName method doesn't return 'Hi my name is [name]' where [name] is the penguin's name


#6

Let me check the lesson and get back to you. ...


#7

I think you're missing some ";"s after your methods

remember your functions need s ";" after the closing "}" brackets


#8

Not so. The syntax is correct. We only add a semi-colon if the function is an anonymous expression:

var func = function () {
    // code
};

#9

This is the code that worked for me,

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

#10

thanks it worked for me.


#11

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

// create a Penguin constructor here
function Penguin(name, numLegs){
this.name = name;
this.numLegs = numLegs;
}
// create a sayName method for Penguins here
Penguin.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
// our test code
var theCaptain = new Penguin("Captain Cook", 2);
theCaptain.sayName();

This worked for me!


#12