25/33 - Could 3 rabbits be just one?


#1


Create a new object rabbit1 with the adjective "fluffy", a new object rabbit2 with the adjective "happy", and a new object rabbit3 with the adjective "sleepy".
Use the method describeMyself to print out in the console a sentence about each object you just created!


There is no problem in this code, rather a challenge. Does anyone see variables "rabbits"? They are just loops, right? Therefore we can make a loop by using "for" method described few lessons before. Any tips or ideas how to resolve this challenge?


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");
rabbit1.describeMyself();
rabbit2.describeMyself();
rabbit2.describeMyself();


#2

what do you mean? something like putting the rabbits into a loop? and then print describeMyself for every rabbit? yea, you can do this:

var rabbit1 = new Rabbit("fluffy");
var rabbit2 = new Rabbit("happy");
var rabbit3 = new Rabbit("sleepy");
rabbits = [rabbit1, rabbit2, rabbit3]
for (i = 0; i < rabbits.length; i++){
  rabbits[i].describeMyself();
}

#3

If I understood your question:

var rabbits = [rabbit1, rabbit2, rabbit3];

for(var i = 0; i < rabbits.length; i++) {
   rabbits[i].describeMyself();
}

#4

Ok, thanks guys, I thought, it can be done without vars, but it's inevitable.


#5

which variables? Please be more specific


#7

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