Question: class AntFarm { / * * * Constructor for the AntFarm class. * * @param { . . . string } ants - Names of

class AntFarm {
/**
* Constructor for the AntFarm class.
*
* @param {...string} ants - Names of the ants.
*/
constructor(...ants){
/** @private */
this.ants = ants.map((name)=>({ name, health: 100}));
}
/**
* Makes the ants in the farm do chores, decreasing their health by 20 points each.
*
* @returns {string} A message indicating the number of ants starting work.
*/
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!`;
}
/**
* Feeds the ants in the farm, increasing their health by 15 points each.
*
* @param {string} name - The name of the ant(s) to feed.
* @returns {string} A message indicating the success or failure of feeding the ants.
*/
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!";
}
/**
* Removes an ant from the farm.
*
* @param {object} ant - The ant object to remove.
*/
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!