A strange result in the output of Inherit Behaviors from a Supertype challange

Tell us what’s happening:
I pass the challenge, but when I put in the console:

console.log(duck.eat());
console.log(beagle.eat());

I saw in the output:

nom nom nom
undefined
nom nom nom
undefined

Why there is undefined two times and where it comes from?

My code so far


function Animal() { }

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

// Only change code below this line

let duck = Object.create(Animal.prototype); // Change this line
let beagle = Object.create(Animal.prototype); // Change this line
console.log(duck.eat());
console.log(beagle.eat());

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Inherit Behaviors from a Supertype

Link to the challenge:

That’s because eat() method doesn’t return anything. The nom nom nom is written to console by the method itself, not from the fact of wrapping method call in console.log(duck.eat()).

It can be written just duck.eat(), the nom nom nom will still appear in console, but undefined will not that way.

1 Like