Question: class AntFarm { constructor ( . . . ants ) { this.ants = ants.map ( ( name ) = > ( { name, health: 1

class AntFarm {
constructor(...ants){
this.ants = ants.map((name)=>({ name, health: 100}));
}
work(){
if (this.ants.length ===0){
return "No ants here. Did you work them to death?";
}
this.ants.forEach((ant)=>{
ant.health -=20;
if (ant.health <=0){
this.removeAnt(ant);
}
});
return `${this.ants.length} ants starting work!`;
}
feed(name){
const antsToFeed = this.ants.filter((ant)=> ant.name === name);
if (antsToFeed.length ===0){
return `${name} not found!`;
}
antsToFeed.forEach((ant)=>{
ant.health +=15;
});
return "Yum!";
}
removeAnt(ant){
const index = this.ants.indexOf(ant);
if (index !==-1){
this.ants.splice(index,1);
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!