25 Constructors in Review


#1

Passed the exercise but I feel I wrote something incorrectly.



I got it correctly, but the console isn't printing the message (ex. I'm expecting to see "I am a happy rabbit" etc).

The only thing on the right screen:

[Function]
[Function]
[Function]


function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
    };
}

// now we can easily make all of our rabbits

var rabbit1 = new Rabbit("fluffy");
var rabbit2 = new Rabbit("happy");
var rabbit3 = new Rabbit("sleepy");

console.log(rabbit1.describeMyself);
console.log(rabbit2.describeMyself);
console.log(rabbit3.describeMyself);


#2

First, inside the function you have to use return not console.log:
return "I am a " + this.adjective + " rabbit";
Second, when you calling the function, you have to add the parentheses:
console.log(rabbit1.describeMyself());


#3

Thank you, macropod, for catching the parentheses mistake! I've made that one myself.

Allow me to disagree with your suggestion to use return instead of console.log inside the function. Although it is an alternative possibility, but that function was provided for us and we were not asked to modify it. So I would leave it as it is. But now we don't have to use console.log again when calling the method because it will do the printing for us.

So we can just say:
rabbit1.describeMyself(); instead of
console.log(rabbit1.describeMyself());

This helped me find my bug!

Hope this helps, therepublicofkang !


#4

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