20/30?


#1

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

Oops, try again. Make sure to properly define the Emperor constructor!


#2

Your code is good. Anyhow try modifying with the below line at the end.

var emperor = new Emperor(2);


#3

Please re-read the full set of 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.

Similar to what we did in the previous exercise, make Emperor inherit from Penguin by setting the prototype of Emperor to be Penguin.

Create a new emperor object that is an instance of the Emperor class with any name you'd like. Then use console.log to print the number of legs emperor has—this should have been inherited from Penguin!


#4

@cssmaster90864,

But in this exercise they are trying to encourage you to think about

J a v a S c r i p t and it's == p r o t o t y p e - C h a i n ==

They want you to set up the chain:

        OBJECT
  Object.prototype
           ^
           |
  Animal.prototype  (( has the sayName Method))
           |                              ^
           |                              | 
           |                    Animal Class-Constructor
           ^                   | with this constructor
           |                   |  you can create Instance's of Animal
           |
           |    <<== You define this link in the Chain using
           ^            Penguin.prototype = new Animal();
           |
  Penguin.prototype ((is empty))
           |               ^
           |               | 
           |          Penguin Class-constructor
           ^                   | with this constructor
           |                   |  you can create Instance's of Penguin
   /Instance /      <---   using new Penguin("aName");
   | of      |
   | Penguin |
    The Penguin-Instance has access
     to Animal.prototype.sayName Method
     via the Penguin-prototype which is
      -chained- to the Animal-prototype.

#5

Your code:
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

[quote="cssmaster90864, post:1, topic:16867, full:true"]
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);

The bold bit is your mistake.

The correction:
var emperor = new Emperor("Any name you want");

Hope this helps :smile:


#6

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("Ron");
console.log(emperor.numLegs);

Hello, this is what i am trying but it keeps giving me an error "Oops, try again. Make sure to log the number of legs emperor has!". Could you please help me with this ?


#7

just refresh the page and its all done..


#8

That code should work:

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

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


#9

ah ■■■■ it i had exactly the same code except the "var emperor = new Emperor();" was missig I just could not get it would be such a small mistake :grinning: