15. Black (white) penguin


#1

I've got a problem that codeacademy returns me error: Oops, try again. Make sure to create a new Penguin instance called penguin!

Here's the code:

// the original Animal class and sayName method
function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};

// define a Penguin class
function Penguin(name){
    Penguin.prototype = new Animal(); 
    this.name = name;
    this.numLegs = 2;
      
}

// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();
var penguin = new Penguin("Pin")
penguin.sayName();

#2

you define penguin class:

function Penguin(name){
    Penguin.prototype = new Animal(); 
    this.name = name;
    this.numLegs = 2;
      
}

you should set it as instance of Animal after defining penguin class, not during defining penguin class


#4

Oh thanks! It's done now. :slight_smile:


#5