20. Penguins, Properties, and the Prototype


#1

I don't know what's wrong...
It seems to be working because it logs "I have 2 legs" to the console, which is the objective, right?
But it still gives me the error "Oops, try again. Make sure to log the number of legs emperor has!"
Here is my code:

function Penguin(name, numLegs) {
    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("Imperador");

console.log("I have " + emperor.numLegs + " legs");

Help me! :persevere:


#2

I've found out what's wrong!
:smiley:

You're not supposed to write anything but "emperor.numLegs" in console.log();
Hahaha! Silly me!

So the correct code is

function Penguin(name, numLegs) {
    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("Imperador");

console.log(emperor.numLegs);

#3

i made this but still wrong :frowning:


#4

I had exactly that to begin with but it DOESN'T work. I just get an error message telling me to be sure and properly define the Emporer constructor.


#5

Note the difference in size of first letter in the word "Emperor".


20. Not working?
#6

This is not solved yet... I also am having the same problem. It seems everything is ok, and the console prints the proper answer,,, but then it keeps saying "Make sure to properly define the Emperor constructor!"


#7

I'm having the same issue, but instead the editor tells me to "Make sure to log the number of legs the Emperor has," but I totally am!


#8

Ahh... sorry folks. Looks like it didn't work because I accidentally spelled it as "emporer" instead of "emperor." It does work :grimacing:


#9

@methodrunner53295 and @sherman_snt may you post a topic about your specific problem along with your code so i may help you? Or have you found the problem yet?


#10

Well, somehow I fixed it, but it was like two days ago so I'm a bit fuzzy on how I fixed it. I feel like it just randomly fixed itself somehow because it doesn't look like I changed anything. My code still looks exactly like srtabebrito's code like it did before but wasn't working then. But now it works.


#11

You have some issus with the semicolon after the first function:

function Penguin(name, numLegs) {
this.name = name;
this.numLegs = 2;
} // here you have to delete the semicolon.

function Emperor(name){
this.name = name;
} // here you have to delete the semicolon.

AND

var emperor = new Emperor() // this one has to be without the ("Imperador")
{
console.log(emperor.numLegs);
}


#12

THis way ANd its working

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();

console.log(emperor.numLegs);


#13

@berdimyrat not really, I've got exactly the same thing and it still says ' Oops, try again. Make sure to log the number of legs emperor has! '

I've spent around 20 minutes on that, looking for typos, errors etc. but cannot really find anything wrong with my code... now I literally just copied yours and pasted into mine and the same error appeared.

@@@@@@@@@@@@@@@@@

OK. Apparently I was lacking 's' in numLegs all time long. However, it doesn't change the fact that @berdimyrat code doesn't work :stuck_out_tongue_winking_eye:

that's what I used:

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);


#14

@bitpro06452
His code does work.

Just tested it in jsbin


#15

I meant it doesn't work for codeacademy to get a pass


#16

how did yours fix itself? I can find no mistake in mine and I received the same message like you. Here is my code:

function Penguin(name, numLegs) {
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();

console.log(emperor.numLegs)


#17

Don't use Chrome to answer this question, use either firefox or newer IE then no more error.


#18

You should use chrome. I copy pasted the code and it worked


#19

I couldn't get it to work until I used Firefox.


#20

SWITCH BROWSERS!!! I didn't change anything other than switching from Chrome to Firefox and it worked