19. Black (and White) Penguin Magic


#1

Getting an error: Oops, try again. Make sure to create a new Penguin instance called penguin!

Can't seem to figure out what's going on? It prints "poop" to the console like it's supposed to..

// 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) {
    this.name = name;
    this.numLegs = 2;
}
// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();
var penguin = new Animal("poop");
penguin.sayName();

#2

try after refreshing your browser :slight_smile:


#3

Try this and see if it works for you because it worked for me really well.
// define a Penguin class
function Penguin(name) {
this.name = name
this.numLegs = 2
};
// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();
var penguin = new Penguin("elton")

penguin.sayName();


#4

function Penguin(name){
this.name=name;
this.numLegs=2;
};
Penguin.prototype = new Animal();
var penguin = new Penguin("Havi");
penguin.sayName();


#5

the message is telling you to "create a new Penguin instance called penguin"

I had the similar yet opposite problem as you, in your code:

var penguin = new Animal("poop");

i was naming the "var penguin" just a random word. What you're doing is with "new Animal" is creating a 'new Animal instance' instead of a 'new Penguin instance'

What i did is change the var to "penguin" and you must change "Animal" to "Penguin"


#6

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