Add Methods After Inheritance The task is not very clear

Tell us what’s happening:
" Add all necessary code so the Dog object inherits from Animal and the Dog's prototype constructor is set to Dog. Then add a bark() method to the Dog object so that beagle can both eat() and bark() . The bark() method should print “Woof!” to the console."

I feel that when it says “Add a bark() method to the Dog’s Object” it is not very clear whether you want me to add the method to the constructor function directly or to the constructor’s prototype.

I did it, it wasn’t hard, but I feel it’s not very clear. If it’s me who misunderstands, I apologize.

Your code so far


function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };

function Dog() { }

// Add your code below this line

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

Dog.prototype.bark = function() {
console.log("Woof!");
}

// Add your code above this line

let beagle = new Dog();

beagle.eat(); // Should print "nom nom nom"
beagle.bark(); // Should print "Woof!"

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36.

Challenge: Add Methods After Inheritance

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance

@ovirex I am inclined to agree other than the fact that the above comment appears below the Dog function, which would mean the only place to add the bark method would be the Dog’s prototype.

That being said, I do think the wording could be made clearer with the following change to the instructions:

and the following change to the third test:

Add all necessary code so the Dog object inherits from Animal and the Dog's prototype constructor is set to Dog. Then add a bark() method to Dog’s prototype so that beagle can both eat() and bark() . The bark() method should print “Woof!” to the console.

Dog should have a bark method that is a prototype property.

What do you think?

1 Like