hi
i have this task to do but im struggling with this second question for the last 6 hours.
these are the instructions:
let’s add composition. Make a new class called Home
. Lots of people have dogs and cats in their homes. Home
should have a function called adoptPet
that takes any Animal
as an input. The new pet should be stored in the Home
object in an array/list. The Home
object should also have a function called makeAllSounds
.
It should work like this:
var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();
home.makeAllSounds();// this doesn't give/return any result/data
home.adoptPet(dog1);
home.makeAllSounds();
// this prints :
// Dog barks
home.adoptPet(cat);
home.makeAllSounds();
// this prints :
// Dog barks
// Cat meows
home.adoptPet(dog2);
home.makeAllSounds();
// this prints :
// Dog barks
// Cat meows
//Dog barks
this is my code so far:
class Animal {
constructor(name) {
this.name = name;
}
eat() {
return `${this.name} eats`
}
sounds() {
return
}
}
class Dog extends Animal{
constructor(name) {
super(name);
this.name = name
}
sounds() {
return "Dog barks"
}
}
class Cat extends Animal{
constructor(name) {
super(name);
this.name = name
}
sounds() {
return "Cat meows"
}
}
class Home extends Animal{
constructor(name) {
super(name);
this.newPet = [];
}
adoptPet(animal) {
this.newPet.push(animal)
}
makeAllSounds() {
sounds();
sounds();
}
}
I tried using separate functions but i cannot use it as stated in the instructions.
please help me compose both animal sounds and help with adding pets to the adoptPet array so it accumulates as shown above how it should output.