Search code examples
javascriptmethodsconstructorprototype

Writing shouldQuarantine prototype in JavaScript


I need this to determine if any of the passengers are isHealthy =false then quarantine the wagon. I may have an issue on the join prototype as well. The isHealthy is only triggered if they eat and have no food. So it is possible for them to eat, and then have no food but not trigger isHealthy.

const Traveler = function (travelerName) {
    this.name = travelerName;
    this.food = 1;
    this.isHealthy = true;
};
Traveler.prototype.hunt = function () {
    this.food += 2;
    console.log(this.food);
};
Traveler.prototype.eat = function () {
    this.food -= 1;
    if (this.food === 1) {
    } else {
        this.food === 0;
        this.isHealthy = false;
    }
    console.log(this.food);
};

console.log(new Traveler("John"));

function Wagon(capacity) {
    this.capacity = capacity;
    this.passengers = [];
}
console.log(new Wagon(4));
Wagon.prototype.getAvailableSeatCount = function () {
    let seatingCapacity = this.capacity - this.passengers.length;
    console.log(seatingCapacity);
    return seatingCapacity;
};

Wagon.prototype.join = function (traveler) {
    console.log(this.capacity);
    let currentCapacity = this.capacity;
    if (currentCapacity <= this.passengers.length) {
        this.currentCapacity = 0;
    } else if (this.getAvailableSeatCount != 0) {
        this.passengers.push(traveler);
    }

    console.log(this.passengers);
};
Wagon.prototype.shouldQuarantine = function () {
    for (let i = 0; i < this.passengers.length; i++) {
        if (this.passengers[i].isHealthy) {
            return false;
        }
    }
};

Wagon.prototype.totalFood = function () {
    let totalFood = "";
    this.passengers.forEach(this.food);
    console.log(this.food);
};

Solution

  • In you eat method of the Traveler class, first check if there is any food. If there is, then subtract one and check if the food is now empty. If it is then set isHealthy to false.

    Traveler.prototype.eat = function () {
      if (this.food > 0) {
        this.food -= 1;
    
        if (this.food === 0) {
          this.isHealthy = false;
        } 
      }
    };
    

    Subsequently you should also modify your hunt method to make your traveler healthy again after hunting.

    Traveler.prototype.hunt = function () {
      this.food += 2;
      this.isHealthy = true;
    };
    

    In the shouldQuarantine method of the Wagon class, instead of checking if all passengers are healthy, check if anyone of them is not healthy and return true if that is the case.

    If everyone is healthy, the loop will finish. Return false after the loop.

    Wagon.prototype.shouldQuarantine = function () {
      for (let i = 0; i < this.passengers.length; i++) {
        if (!this.passengers[i].isHealthy) {
          return true;
        }
      }
    
      return false;
    };
    

    Alternatively you could use the some method on the this.passengers array to check if any of the passenger isn't healthy.

    Wagon.prototype.shouldQuarantine = function () {
      return this.passengers.some(
        passenger => passenger.isHealthy === false
      );
    };
    

    The join method can be simplyfied. You only need the result from this.getAvailableSeatCount() to see if there is any room. Add the traveler if the result is not 0.

    Wagon.prototype.join = function (traveler) {
      const availableSeats = this.getAvailableSeatCount();
      if (availableSeats !== 0) {
        this.passengers.push(traveler);
      }
    };
    

    I also noticed that the totalFood method doesn't work as expected, but I'll let this one up to you. Hint: totalFood should be a number. Loop over every passenger and add the amount of food to the totalFood value.