15.17 Marching Penguins (solution + tip)


#1


https://www.codecademy.com/en/courses/objects-ii/3/2?curriculum_id=506324b3a7dffd00020bf661


Though the code passes, it displays nothing on the results window.
If you are having the same issue, or even not passing though you've verified over and over, be sure to go to jsFiddle.net and input your code in the JavaScript box. Ensure that you open a browser console window so you can see the results. If it works there, chances are the page is just sticking. Reload a couple of times. That's how I got it to work for me. -- Suppose it doesn't matter now, new JS course very soon :wink:


function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};


// create a Penguin constructor here
var Penguin = function (name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
};
    
// create a sayName method for Penguins here
Penguin.prototype.sayName = function () {
    console.log("Hi my name is " + this.name);
}

// our test code
var theCaptain = new Penguin("Captain Cook", 2);
theCaptain.sayName();


#2

Is Penguin a constructor, when it starts with var? Penguin in this case is an object I think, and the function() that follows is the constructor, which makes the Penguin object (var). I think this is obsolete, as you only need to code the constructor in that block, as on the bottom, you create a new object through the "new <constructor" keyword.


#3

function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};


// create a Penguin constructor here
function Penguin(name, numLegs){
        this.name = name;
        this.numLegs = numLegs;
    };

// create a sayName method for Penguins here
Penguin.prototype.sayName = function() {
        console.log("Hi my name is " + this.name);
};

// our test code
var theCaptain = new Penguin("Captain Cook", 2);
theCaptain.sayName();

#4

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