Question: class AntFarm { / * * * Constructor for the AntFarm class. * Creates an instance of AntFarm with an array of ants. * *

class AntFarm {
/**
* Constructor for the AntFarm class.
* Creates an instance of AntFarm with an array of ants.
*
* @param {...string} antNames - Names of the ants.
*/
constructor(...antNames){
/** @private */
this.ants = antNames.map((name)=>({ name, health: 100}));
}
/**
* Method to make the ants do chores.
* Decreases the health of each ant by 20.
* Removes any ant with health <=0.
*
* @returns {string} A message indicating the number of ants starting work or if there are no ants left.
*/
work(){
if (this.ants.length ===0){
return "No ants here. Did you work them to death?";
}
this.ants.forEach((ant)=>{
ant.health -=20;
});
this.ants = this.ants.filter((ant)=> ant.health >0);
return `${this.ants.length} ants starting work!`;
}
/**
* Method to feed ants and increase their health.
*
* @param {string} antName - The name of the ant(s) to feed.
* @returns {string} A message indicating if the ant(s) were found and fed.
*/
feed(antName){
const antsToFeed = this.ants.filter((ant)=> ant.name === antName);
if (antsToFeed.length ===0){
return `${antName} not found!`;
}
antsToFeed.forEach((ant)=>{
ant.health +=15;
});
return "Yum!";
}
}

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!