20/30 Penguins, Properties, and the Prototype


#1

Can some one tell me where i am going wrong here:

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

// create your Emperor class here and make it inherit from Penguin
function Emperor(name) {
this.name = name;
}
// create an "emperor" object and print the number of legs it has
Emperor.prototype = new Penguin();

var emperor = new Emperor("bob");

console.log(emperor.numLegs);`


#2

You have an apostrophe before and after all of the code. The rest looks fine.


#3

Try:
function Penguin(name) {
this.name = name;
this.numLegs = 2;
}

// create your Emperor class here and make it inherit from Penguin
function Emperor(name) {
this.name = name;
}

// create an "emperor" object and print the number of legs it has

Emperor.prototype = new Penguin();
var emperor = new Emperor("Beeky Bob");
console.log(emperor.numLegs);


#4

#5

Looks a little longwinded, can just use;

Emperor.prototype = new Penguin();

var emperor = new Emperor("Pingu");
console.log(emperor.numLegs);

#6

Hi! I have a problem, it says "Oops, try again. Make sure to log the number of legs emperor has!"
here is my code:


#7

You are missing
this.numLegs = 2; from your Emperor function


#8

The instructions tell you specifically to NOT include a definition for numLegs in the Emporer constructor. The point here is to demonstrate inheritance of the numLegs property from the parent class, Penguin.

From the instructions:
"Create an Emperor class that takes a single name parameter and sets its name property to be this value. Don't set a numLegs property in the constructor."

I had the same problem but in my case I was spelling Emperor as Emporer.

Elsewhere in the forums some people (who didn't have my spelling errors) resolved the problem by going to a new tab and running their code and then getting the green success notification.

The Codecademy JavaScript Labs facility is great for testing your code but, of course, it invoking the unit testing functions.

Coop, aa1ww


#10

Hi All: How about helping me out here. This doesn't work
Getting error: "Make sure to log the number of legs emperor has!"

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

// create your Emperor class here and make it inherit from Penguin
function Emperor(name) {
this.name = name;
}
Emperor.prototype = new Penguin();
// create an "emperor" object and print the number of legs it has
var emperor = new Emperor("Bob");
console.log(emperor.numLegs);


#11

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

// create your Emperor class here and make it inherit from Penguin
function Emperor(name) {
this.name = name;
}

// create an "emperor" object and print the number of legs it has
Emperor.prototype = new Penguin()

var emperor = new Emperor("Olovor");

console.log(emperor.numLegs);

This worked for me @ken.willmott


#12

This didn't work for me.


#13

I found out that this is a Crome vs IE issue. I switched to IE and the same code works fine.


#14

Need help on number 21

// original classes
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
this.isAlive = true;
}
function Penguin(name) {
this.name = name;
this.numLegs = 2;
}
function Emperor(name) {
this.name = name;
this.saying = "Waddle waddle";
}

// set up the prototype chain
Penguin.prototype = new Animal();
Emperor.prototype = new Penguin();

var myEmperor = new Emperor("Jules");

console.log(myEmperor.saying); // should print "Waddle waddle"
console.log(myEmperor.numlegs); // should print 2
console.log(myEmperor.isAlive); // should print true

My error is
Oops, try again. It looks like you didn't print 2


#15

Hi in this console.log() you put numlegs instead of numLegs


#16

Silly mistake sorry
Thank you!


#17

remove the ' from the last line.


#18

I had ths same problem too.
Thanku. It hekped a lot!


#19


#20

(Yes)
var myEmperor = new Emperor("myName");
console.log(myEmperor.numLegs);

(Not)
var emperor = new Emperor("myName");
console.log(emperor.numLegs);

:+1: