28/30 - Why doesn't this work!? Works correctly in JS console


#1

Why doesn't this work?!

It prints to Codecademy console correctly, and also prints correctly to Javascript console in Chrome.

I tried refreshing the page, but it's still same error.

The error:

Oops, try again. It appears that your sayHello method doesn't properly log to the console 'Hello this is a [breed] dog' where [breed] is the breed of the Dog

The code:

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

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

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

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


#2

Try using
console.log("Hello this is a " + this.breed');
as the code checker is checking on this EXACT outcome....


#3

I changed it to:

console.log("Hello this is a " + this.breed);

It didn't work, so I changed it back to below & it worked! Thx so much !!

console.log("Hello this is a " + this.breed + " dog");


#4

The culprit is actually the '\n' I initially entered before:

console.log("Hello this is a " + this.breed + " dog\n");


#5

This seems to be ...

++++ reset of session *****************
under certain circumstances you can shoot your Browser in an
inconsistent state.

Therefor it is of an advantage to know that you have 2 reset facilities:

One is the use of the F5-key which does a refresh Browser

and

Two, select&copy your code
Then use the Reset Code button of the course-window,
then paste your code back in.

Addendum from Tony de araujo
General Notes:
Always refresh the browser after making corrections:
CTRL f5 ( if on Windows or Linux)
CMD r ( if on a MAC).


#6

This is what ultimately solved the problem... this particular section could use some more wiggle room....

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

// add the sayHello method to the Dog class 
// so all dogs now can say hello
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();

#7

What you should be aware of,
that you could have written the code in a different order
like:

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

var yourDog = new Dog("golden retriever");
var myDog = new Dog("dachshund");

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

#8

I checked my code several times and made sure i put in the right syntax, after also refreshing and re-opening the page, and it keeps telling me TypeError: Cannot set property 'sayHello' of undefined Oops, try again. Make sure to define the Dog.prototype.sayHello method! so I have no idea what is defective. Thanks.


#9

I have the same problem, have you found a workaround?


#10

If you run in a TypeError
++++ reset of session *****************
As you are manipulating with objects
under certain circumstances you can shoot your Browser in an
inconsistent state.

Therefor it is of an advantage to know that you have 2 reset facilities:

One is the use of the F5-key which does a refresh Browser

and

Two, select&copy your code
Then use the Reset Code button of the course-window,
then paste your code back in.

Addendum
General Notes:
Always refresh the browser after making corrections:
CTRL f5 ( if on Windows or Linux)
CMD r ( if on a MAC).


#11

Thanks for the tip mate! I refreshed my browser, pasted it back in and it worked!