I am learning constructors, Im trying to work through this exercise. I am trying to follow these instructions. But I cant get the entire program to work. I am using instanceof to verify the coin object. I am wondering if there is another way to do this. I hope the instructions are clear enough.
COIN OBJECT: Should have 1 property and 2 methods. Properties: State - this should be 1 or a 0 heads(1) or tails (0) Methods: flip() - This should randomly assign a 0 or a 1 to the state property toString()Based upon the state, this should return the string "heads" or "tails"
CHICKEN COOP OBJECT: secod constructor function, create an chicken coop object the chicken coop should have one parameter ti the Coop functionm numChickens Coop needs one property and one method Properties: numChickens - the total number of chickens. Methods: gatherEggs() - return a random number 1-3 x numChickens Add the chicken coop properties and methods here
CHICKEN FEED VENDING MACHINE: Two properties and one method Properties: food - The amount of food in the vending machine. This should start at 12. numCoins - number of coins put into the machine. This should start at 0. Methods: dispence(coin) - dispense 5 feed for every coin passed to the method. The coin parameter, should be a coin object from the Coin constuctor above. If coin is valid AND there is 5 food AND add +1 numCoins. Return the number 5 or the remaining food in the machine. If food is 0 return 0
/** 1. Coin Constructor
* Fill in the properties and methods inside of the coin function */
const Coin = function() {
state: 0,
flip: function () {
this.state = Math.floor(Math.random() * 2) == 0 ? "tails" : "heads";
},
toString: function () {
if (this.state === 0) {
sides = "heads";
} else if (this.state === 1) {
sides = "tails";
}
return sides;
},
};
/** 2. Chicken Coop Constructor,
* fill in the properties and methods inside of the Coop function */
const Coop = function (numChickens) {
this.numChickens = numChickens;
this.gatherEggs = function () {
return Math.floor(Math.random() * 3 + 1) * numChickens;
};
};
/** 3. Feed Vending Machine Constructor,
* fill in the properties and methods inside of the FeedMachine function */
const FeedMachine = function () {
//Add the feed vending machine properties and methods here
this.food = 12;
this.numCoins = 0;
this.dispense = function (coin) {
if (coin instanceof Coin && this.food > 5) {
this.food -= 5;
this.numCoins += 1;
return 5;
} else if (!Coin) {
return 0;
}
if (this.food === 0) {
return 0;
} else if (this.food < 5) {
this.numCoins++;
const restOfFood = this.food;
this.food -= this.food;
return restOfFood;
}
};
};
export { Coin, Coop, FeedMachine };