Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions