Skip to content

Instantly share code, notes, and snippets.

@ayoformayo
Created July 8, 2013 02:11
Show Gist options
  • Save ayoformayo/5945765 to your computer and use it in GitHub Desktop.
Save ayoformayo/5945765 to your computer and use it in GitHub Desktop.
function FruitTree(){
this.height = 0;
this.age = 0;
this.fruitCollection = [];
this.dead = false;
}
function Fruit(){}
FruitTree.prototype.grow = function(){
this.age += 1;
if(this.age >= this.mature && this.age < this.peak){
for(i = 0; i < this.output; i++){
this.fruitCollection.push(this.fruit);}
}
else if(this.age > this.peak){
this.fruitCollection = [];
this.dead = true;
console.log ('The tree is dead!');
}
else {
this.height += this.yearlyGrowth;
}
};
function OrangeTree(){
FruitTree.call(this);
this.mature = 3;
this.fruit = Orange;
this.peak = 10;
this.output = 4;
this.yearlyGrowth = 5;
}
OrangeTree.prototype = new FruitTree();
OrangeTree.prototype.constructor = OrangeTree;
function Orange(){
Fruit.call(this);
this.diameter = (Math.floor(Math.random()*3)+1);
}
Orange.prototype = new Fruit();
Orange.prototype.constructor = Orange;
function PearTree(){
FruitTree.call(this);
this.mature = 4;
this.fruit = Pear;
this.peak = 9;
this.output = 2;
this.yearlyGrowth = 4;
}
PearTree.prototype = new FruitTree();
PearTree.prototype.constructor = PearTree;
function Pear(){
Fruit.call(this);
this.diameter = (Math.floor(Math.random()*5)+1);
}
Pear.prototype = new Fruit();
Pear.prototype.constructor = Pear;
function AppleTree(){
FruitTree.call(this);
this.mature = 2;
this.fruit = Apple;
this.peak = 8;
this.output = 3;
this.yearlyGrowth = 3;
}
AppleTree.prototype = new FruitTree();
AppleTree.prototype.constructor = AppleTree;
function Apple(){
Fruit.call(this);
this.diameter = (Math.floor(Math.random()*6)+1);
}
Apple.prototype = new Fruit();
Apple.prototype.constructor = Apple;
var treeGrove = {
treeCollection : [],
growTrees : function(){
for (var i=0;i < this.treeCollection.length;i++)
{
this.treeCollection[i].grow();
}
},
addTree : function(tree){
this.treeCollection.push(tree);
},
trees : function(){
return this.treeCollection;
},
mature : function(){
for (var i=0;i < this.treeCollection.length;i++)
{
if(this.treeCollection[i].fruitCollection != []){
console.log(this.treeCollection[i]);}
}
},
dead : function(){
for (var i=0;i < this.treeCollection.length;i++)
{
if(this.treeCollection[i].dead === true){
console.log(this.treeCollection[i]);}
}
}
};
var orange = new OrangeTree();
var pear = new PearTree();
var apple = new AppleTree();
treeGrove.addTree(orange);
treeGrove.addTree(pear);
treeGrove.addTree(apple);
treeGrove.growTrees();
treeGrove.growTrees();
treeGrove.growTrees();
treeGrove.growTrees();
treeGrove.growTrees();
treeGrove.growTrees();
treeGrove.growTrees();
treeGrove.growTrees();
treeGrove.growTrees();
treeGrove.growTrees();
treeGrove.dead();
treeGrove.growTrees();
console.log(treeGrove.treeCollection[0].fruitCollection);
console.log(treeGrove.treeCollection[0].age);
console.log(treeGrove.treeCollection[1].age);
console.log(treeGrove.treeCollection[2].age);
console.log(treeGrove.treeCollection.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment