28.) Hello? Yes, This is Dog. Did I cheat? Or am I right?


#1

Ok my whole code is down below but the main section of code for this lesson is this part below.

Link to Lesson: https://www.codecademy.com/en/courses/objects-ii/5/2?curriculum_id=506324b3a7dffd00020bf661#

var sayHello = new Dog("beagle");
Dog.prototype.sayHello = function() {
    console.log("Hello this is a " + this.breed + " dog");
};

I was mainly wondering if I went about this the right way. I passed the lesson with no problem but the instructions give this: "sayHello should print to the console: "Hello this is a [breed] dog", where [breed] is the dog's breed" So, I wasn't sure if I was supposed to do something with [breed] in the sentence or prep something before so that the breed would show up in [breed] or if what I did was fine. I just felt like I was going around what the instructions were saying to do but it worked perfectly as I had it so yeah just would like some clarity.

function Dog (breed) {
    this.breed = breed;
};

// add the sayHello method to the Dog class 
// so all dogs now can say hello
var sayHello = new Dog("beagle");
Dog.prototype.sayHello = function() {
    console.log("Hello this is a " + this.breed + " dog");
};

var yourDog = new Dog("golden retriever");
yourDog.sayHello();

var myDog = new Dog("dachshund");
myDog.sayHello();



#2

That's how I wrote my code. Minus var sayHello = new Dog("beagle");. I'm not too sure why that's there. I don't consider it cheating (but then, I'm not too sure what you mean by cheating if you were following the instructions).
The console should output

Hello this is a golden retriever dog
Hello this is a dachshund dog

#3

So I think I understand your question. You're asking if you used this.breed correctly, right?
Yes, you did. :slight_smile:
The hint even says

To access a dog's breed from within the method, use this.breed.

I know it can be kind of confusing, but if you continue to do Object Oriented Programming with JavaScript, you'll start understanding the this keyword a little more. Tell me if you want some clarity on that, I know it's tough to wrap your head around.

But let's think about something else for a second.
You said that sayHello is an object, (a new Dog), but then you say that sayHello is a function.. Which one do you think it is?
It's the function, because that's the most recent place you defined it.

var sayHello = new Dog("beagle");

You don't need this line at all because you overwrite it. But I am wondering why you have it?


#4

Ok so both @kyleaw and @datfatcat thanks for responding! To answer both of you I was basing my model for this lesson off of the previous lesson dealing with Snoopy and a bark method to override everything with Woof! in Teach Snoopy How do Classes Help us etc. Also, I wasn't sure if I need to define sayHello to be able to use it in my prototype statement. I realize it really doesn't do much....if anything at all xD but I guess I added it in as like a precautionary measure just to make sure...which I guess as both of yall said and explained I don't need anyways!

And yeah my main question was not really did I use this.breed right because I knew what I did was "coding wise" correct I just wanted to make sure that was what the instructions were trying to get me to do in this case. I realize I didn't use [breed] like they said to print in the instructions and usually when they give you something very specific to print like in previous lessons such as Code Your Own Adventure 1 and 2 you better have that exactly as is. Also, I have solved lessons before that worked and passed in the lesson but were not correct in the fact that that is not what the lesson was actually trying to get me to do I just skirted around it like in the infamous FizzBuzz lessons. So all in all thanks! :smiley:

Oh yeah and @datfatcat my console screen shows it perfectly I get:

Hello this is a golden retriever dog
Hello this is a dachshund dog

#5

function Dog (breed) {
this.breed = breed;
};

Dog.prototype.sayHello = function() {
console.log("Hello this is a " + this.breed + " dog")
};

var yourDog = new Dog("golden retriever");
yourDog.sayHello();

var myDog = new Dog("dachshund");
myDog.sayHello();

Here is my code. The lesson passed but the console doesn't print the sayHello dialogue. I am wondering if anybody can tell me why.


#6

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